Convert Null Value to String - C#.NET

Posted by peace on Stack Overflow See other posts from Stack Overflow or by peace
Published on 2010-05-23T10:50:54Z Indexed on 2010/05/23 11:00 UTC
Read the original article Hit count: 278

Filed under:
|
|
|
    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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET