Thursday, July 29, 2010

LINQ {"Specified cast is not valid."} The value for column in table is DBNull.

Issue: While Querying the Dataset using LINQ Query, you get an error "{"Specified cast is not valid."} The value for column in table is DBNull."

Resolution: It it just the way you've written the query that did not handle the Nullable Columns. This write up also illustration on how to convert the Dataset to a list

Changed the code to
List <> output = new List <> ( ) ;
output = (from b in bBaseDataSet.bTable
select new BOuput
{
A = b.a,
B = b.Field <> ("bColumnName"), //remove the extra spaces added
C = b.Field <> ("cColumnName"),
D = b.d
}
).ToList();

No comments:

MSDN: U.S. Local Highlights