By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,707 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Test for empty Dataset field

P: n/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
Aug 18 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Aug 18 '06 #2

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

Marc
Aug 18 '06 #3

P: n/a
AAJ
Thanks Marc

DBNull is just what I needed

Andy
"Marc Gravell" <ma**********@gmail.comwrote in message
news:uW**************@TK2MSFTNGP04.phx.gbl...
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

Aug 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.