Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the correct way to read from a datarow if the cell might be null

Tags:

c#

null

datarow

I have the following code which seems to blow up if the column in the datarow (dr) is null. what is the correct way to parse out the value from the data row and handle null checks?

Person person = new Person()
{
    FirstName = dr["FirstName"].ToString(),
    LastName = dr["LastName"].ToString(),
    BusinessPhoneNumber = dr["BusinessPhone"].ToString(),
like image 880
leora Avatar asked Nov 21 '09 14:11

leora


People also ask

How do I check if DataRow is null?

IsNull(DataColumn, DataRowVersion) Gets a value that indicates whether the specified DataColumn and DataRowVersion contains a null value.

What is the method used to return the DataRow?

You use DataTable's NewRow method to return a DataRow object of data table, add values to the data row and add a row to the data Table again by using DataRowCollection's Add method.

How do you make a DataRow?

To create a new DataRow, use the NewRow method of the DataTable object. After creating a new DataRow, use the Add method to add the new DataRow to the DataRowCollection. Finally, call the AcceptChanges method of the DataTable object to confirm the addition.


3 Answers

If the column is of type string, but is nullable, what about trying:

// FirstName must allow null
FirstName = dr["FirstName"] as string

or

// FirstName would be empty for a NULL in the database
FirstName = (dr["FirstName"] as string) ?? string.Empty 
like image 56
João Angelo Avatar answered Oct 11 '22 01:10

João Angelo


I believe, the data cell beeing null cannot be the reason for your problem. Maybe the column doesn't exist, or any other error happened, or the DataRow itself is null and you should handle that. Look at the exception - "seems to blow up" is not a valid description of your problem.

The following explains that, but will also answer the question from the title for everyone else.

If the column value is null, an object System.DBNull is returned, and .ToString() returns an empty string, while (string) or as string return null.

So it makes no sense to check the returned item for ==null because that won't ever evaluate to true.

  • If you accept empty strings as result, your code is already optimal, no need to handle the DBNull case.
  • If you want to get null, change .ToString() to as string.
  • If you want to handle it in any other way, use if (dr.IsNull("FirstName")) or do ==null on the target variable after the as string.
like image 34
maf-soft Avatar answered Oct 11 '22 03:10

maf-soft


dr["FirstName"].ToString() will occasionally throw null pointer exceptions because you're trying to access ToString on a null object. Other than that, strings can be null, so really, if you know it's a string, and you don't mind that it's null, it'd be safe to do

FirstName = (String) dr["FirstName"]
like image 40
David Hedlund Avatar answered Oct 11 '22 02:10

David Hedlund