Test for empty Dataset field

A

AAJ

Hi all

A quick question on Datasets.....

I populate a dataset from a SQL SERVER 2000 database via a data adapter. I
then use the dataset to populate member variables of my class.

I know the datatype of a column in the database, so I use convert. to
convert the results in to the same datatype within my class i.e.

m_TimeToAction = Convert.ToDecimal(currentRow["TimeToAction"]);
where m_TimeToAction is a Decimal.

The problem is when the dataset field is empty (not null) i.e. has a value
of {}.

Is there a safe way of testing for this condition for all the fields. For
instance, if an empty record definitely returned null I could use

if (currentRow["TimeToAction"] != null) m_TimeToAction =
Convert.ToDecimal(currentRow["TimeToAction"]);

but obviously, as the result isn't guaranteed to be null, the above won't
always work!

I know I could test the length, and also compare to '', but I am looking for
the best all encompassing way to ensure the returned data is valid

any thoughts most welcome

Andy
 
M

Marc Gravell

OK, you've checked for /object/ null, but have you checked for DBNull? you
can do this by either:
currentRow.IsNull(...) // 4 overloads
or
currentRow["FieldName"] is DBNull
(there is also the singleton DBNull.Value)

(i.e. database null is *not* necessarily represented by object null)

If this isn't the problem, what /exactly/ is the value? (what type,
contents, etc) (i.e. look at it in a watch windowin the debugger)

Marc
 
M

Marc Gravell

Also, if the field is actually a string-type that is containing an empty
string, you could test using string.IsNullOrEmpty(blah)

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top