VB.Net - checking for Null values in DataSets

G

Guest

Hi there.

I wonder if there was a "correct answer" for Vb.Net (I understand this is
quite easy in C#).

If im drawing data from a database using a TableAdapter the data ends up in
a DataTable. I then need to iterate through the DataTable to get the values
out. This in itself is not a big issue.

Code something like...

For x = 0 to........

myString = DataTable.Rows(x).Item("MyValue")

until I come across a Null value.

A Null value in the Database throws an exception which is easy to address
with a Try Catch statement. However as mentioned in some of the excellent
articles this is not a good solution for the problem.

Is there some means of testing the value before accessing it so that the
Try/Catch would no longer be required.

Perhaps something like:

If Not Datatable.Rows(0).Item("MyValue") = Nothing Then

myString = DataTable.Rows(0).Item("MyValue")

End If

Which doesnt work!

Try/Catch just seems the wrong approach.


Regards

Martyn Fewtrell
 
C

Cowboy \(Gregory A. Beamer\)

Compare to DBNull.Value.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
 
G

Guest

Thanks to both of you.

I've gone with

If Not (IsDBNull(DataTable.Rows(x).Item("MyValue"))) Then
myString = DataTable.Rows(x).Item("MyValue")
End If

Which seems to do the trick!
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top