Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for null values in C#

Tags:

c#

dataset

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!

like image 1000
jqwha Avatar asked Jun 08 '11 15:06

jqwha


1 Answers

It is kind of strange not to care about the Table or the Column.

It is a much more normal practice to expect table[0].Rows.Count == 0 for instance.

And the best way to check for NULL values is with if(...) ... else ....
The worst way is to wait for Exceptions (in whatever way).

like image 110
Henk Holterman Avatar answered Nov 05 '22 16:11

Henk Holterman