I have a dataset returned by a stored procedure, and one of the elements in it could potentially be null. I am trying to convert every row in a dataset to a strongly typed object, but I can't seem to properly assign a null value.
I created a layout for my script as follows:
DataSet ds = new DataSet(); ds.Tables.Add(new DataTable()); ds.Tables[0].Columns.Add("Name", typeof(string)); ds.Tables[0].Columns.Add("Amount", typeof(decimal)); ds.Tables[0].Rows.Add("My Name Here", null); //create row with a null item DataRow dataRow = ds.Tables[0].Rows[0]; Person p = new Person { Name = (string)dataRow["Name"], Amount = (decimal)dataRow["Amount"] }
Unfortunately, I get the following exception: System.InvalidCastException: Specified cast is not valid.
If I try to use the NULL type (decimal?), I get this error: System.NotSupportedException: DataSet does not support System.Nullable<>.
In the debugger, I performed the following tests for the value in dataRow ["Amount"]:
dataRow["Amount"] is decimal (false) dataRow["Amount"] is decimal? (false) dataRow["Amount"] == null (false) dataRow["Amount"] is object (true)
All I can install is that it is some kind of object ... which is not particularly useful.
Can any of you determine what I am doing wrong?
source share