Alex Rivera | Logout

Test for null values in C#

Asked 2011-06-08T15:01:48.100
12

If I do something like:

DataSet ds = GetMyDataset();

try
{
    string somevalue = ds.Tables[0].Rows[0]["col1"];
}
catch
{
    //maybe something was null
}

Is there a good way to check for null values without using the try/catch? It's just that I don't care if the value in "col1" is null, OR if "col1" didn't exist, OR if there were no rows returned, OR if the table doesn't exist!

Maybe I should care? :) Maybe try/catch is the best way of approaching this but I just wondered if there was another way to do it?

Thanks!

Edit
Report

1 Answer

-1

You check for DBNull.Value e.g.

if (ds.Tables[0].Rows[0]["col1"] != DBNull.Value)
answered 2011-06-08T15:04:34.340

Your Answer