Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Test for empty Dataset field

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AAJ - 18 Aug 2006 08:11 GMT
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
Marc Gravell - 18 Aug 2006 09:27 GMT
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
AAJ - 18 Aug 2006 12:06 GMT
Thanks Marc

DBNull is just what I needed

Andy

> OK, you've checked for /object/ null, but have you checked for DBNull? you
> can do this by either:
[quoted text clipped - 9 lines]
>
> Marc
Marc Gravell - 18 Aug 2006 09:30 GMT
Also, if the field is actually a string-type that is containing an empty
string, you could test using string.IsNullOrEmpty(blah)

Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.