Nullable types & Reflection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all.
First of all, I am not sure this is the place to post my question, but I
didn't find a more apropriate one.
The problem: I try to fill some fields of an object with values from a
database.
Because some database fields are nullable, I use nullable types in my object.
So, to set the value of an int? I write:
FieldInfo field; // age
DataRow dataRow;
//int? man.age
....
field.SetValue(man, dataRow["age"], null);

This code throws an error.
Can anybody help?
Thanks.
 
From your code it does not reflect exact idea what you are trying to do.
I would suggest you to use the following code to handle NULLABLE values to
cast them as ZERO or as empty string

at Stored Procedure level you can use the following while returning a column

Isnull(age, "") as Age


OR

You can write the following code in your code

dataRow("age") = NullableValue(dataRow("age"))


Function NullableValue (obj as Object) as Object
If isDBNULL(obj) then
return ""
else
return obj
end if

end function


Regards,

BizWorld
 
Hi!

I guess that you are doing this when you are retrieving records from a db!?

I tend to do it this way:

This ex. is using a SqlDataReader (SqlRdr)

if (SqlRdr.HasRows)
{
while(SqlRdr.Read())
{
if (SqlRdr["DBColumn"] != System.DBNull.Value)
{
MyObject.MyProperty = SqlRdr.GetInt16(0); // <--The zero based column
ordinal
}
}
}
 
Back
Top