Object must implement IConvertible.

Ever have this error when you were building a master-detail view with the ASP.Net DetailsView and the GridView?

Server Error in ‘/’ Application.


Object must implement IConvertible.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Object must implement IConvertible.

 

I had. You know why? I will explain. It occurs when you use ObjectDataSources to create the master-detail view<< Er moet tussen dat de objectdatasource een methode als selectmethode gebruikt die een string verwacht en daardoor een IConvertible verwcaht>>. When you make the master-detail view the GridView DataKeyNames array contains the primary key fields of the table it displays. The DataKeyNames array is used the fill the SelectedValue property with the primary key value belonging to the row that is selected. The SelectedValue property is used as a parameter of the selectquery of the DetailsView’s ObjectDataSource. This way the GridView and the DetailsView are synchronized. The SelectedValue’s datatype must implement the IConvertible interface. I was using a Guid as the primary key field. A Guid does not implement the IConvertible interface. So the application blows up.

How can we use a Guid field as a primary key? 

Solution 1: use a guid as a parameter.

Solution 2: use the string but convert the guid to a string.