views:

550

answers:

3
    foreach (PropertyInfo PropertyItem in this.GetType().GetProperties())
    {
        PropertyItem.SetValue(this, objDataTable.Rows[0][PropertyItem.Name.ToString()], null);
    }

In one of the loops i get this exceptional error:

Object of type 'System.DBNull' cannot be converted to type 'System.String'.

The error occurs because one of the fields in the database has no value (null), so the string property could not handle it. How can i convert this null to string?

I got this solution

If you know a shorter or better one, feel free to post it. I'm trying to avoid checking on every loop is current value is null or not.

+1  A: 

There's no solution other than checking for null where there's a dereference or method call.

If PropertyItem.Name can be null or PropertyItem.SetValue can't accept a null value then you have to check on every loop.

ChrisF
This seems not to be the problem - DBNull.Value.ToString() works fine and returns an empty string. Property.SetValue() seams to cause the exception when it tries to set a property of type string to DBNull.Value. The check should probably be objDataTable.Rows[0][PropertyItem.Name.ToString()] == DBNull.Value.
Daniel Brückner
@Daniel - I used `PropertyItem.Name` being null as an example. I'll correct the answer
ChrisF
A: 
foreach (PropertyInfo PropertyItem in this.GetType().GetProperties())
    {
        string strValue = objDataTable.Rows[0][PropertyItem.Name.ToString()] == DbNull.Value ? String.Empty : objDataTable.Rows[0][PropertyItem.Name.ToString()].ToString();
        PropertyItem.SetValue(this, strValue, null);
    }
Ben Robinson
It is `objDataTable.Rows[0][strValue]` that is `DbNull` not `PropertyItem.Name` I think.
Martin Smith
I have updated my answer on that basis. This is a bit of a hack though, encapsulating this in a method would be better.
Ben Robinson
Hopefully I'm right then! PropertyItem.Name is reflection oriented rather than DB oriented so I'd be surprised if it would use DbNull.Value rather than just null.
Martin Smith
Gotta love the ?? operator and cut that thing in 1/2
kenny
@Martin Im pretty sute you are right. PropertyInfo.Name will never return DBNull.Value.
Rune FS
@kenny I don't think the ?? operator works with dbnull.value only with real nulls
Ben Robinson
+3  A: 

Ben Got it almost right (I actually think it's just a "typo" from his side but I can't edit it for him). This should do the trick.

foreach (PropertyInfo PropertyItem in this.GetType().GetProperties())
    {
        var value = objDataTable.Rows[0][PropertyItem.Name.ToString()];
        PropertyItem.SetValue(this, value == DBNull.Value ? "" : value.ToString() , null);
    }

And you need to test en every iteration, since it's the value in a given cell in a given row there's really no way to avoid checking every one of those cell values before they are used

Rune FS
Would the coalesce operator work to shorten it a bit `(value as string) ?? string.Empty` (genuine question I don't know the answer)
Martin Smith
unfortunately it wouldn't it's strictly for null-values and since DBNull.Value is an object it won't work. But it'a good comment I like it
Rune FS
can't edit the post due to a SO error so ill comment in stead. If you hjust want to use empty string you could just call .ToString() on value in the above example SetValue(this, value.ToString(),null)
Rune FS