Tuesday, March 27, 2012

Filling a DataTable from SqlQuery : If SqlQuery returns null values problem ocurrs with Da

Filling a DataTable from SqlQuery : If SqlQuery returns some null values problem ocurrs with DataTable.

Is it possible using DataTable with some null values in it?

Thanks

It's perfectly fine to fill a datatable with some (or all) NULL values... the problem is probably that you are trying to use a field that is not there (because it's null).

Example:

DataTable myDT = new DataTable();

myDataAdapter.Fill(myDT); // <-- there's no problem if there are nulls in there

myDT.Rows[3]["SomeFieldWhichCanBeNull"].ToString(); // <--BANG, there's your error!

|||

You can store the NULL values and to avoid the error, you may check Is it null or not by usingIsNull, like in this example:

1if (!dr.IsNull("MyColumn")2{3 MyColumn = dr["MyColumn"].ToString();4}5

Good luck.

No comments:

Post a Comment