0

I have found in my codebase lots of places has been coded as

 dr["FIRST_ITEM"].Trim()

Now the problem is that if FIRST_ITEM column is a allow null values there is chance to encounter Null pointer Exception. I know, I can change the code to

Convert.ToString(dr["FIRST_ITEM"]).Trim()

in order to fix the problem. But then I have to do this in 1000 lines of code. Can I have solution, by which I can fix the problem with least effort?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1672097
  • 361
  • 1
  • 4
  • 12

1 Answers1

0

I just ended up writing a quick method like this:

  private static string GetS(object obj)
        {
            if (obj == null || obj == DBNull.Value)
                return "";

            return obj.ToString();

        }

Example:

MyObject.LastName = GetS(dr["LastName"]));

Someday I'll have time to go back and write an extention to the DataRow or to write a generic converter. But for now this works and I have one for dates and bool data types.

JBrooks
  • 9,901
  • 2
  • 28
  • 32