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

VB.Net - checking for Null values in DataSets

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


P: n/a
Martyn trying checking fo DBNull.value
Hope that helps
Patrick

"Martyn Fewtrell" <mf*******@newsgroup.nospamwrote in message
news:88**********************************@microsof t.com...
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

Aug 1 '06 #2

P: n/a
Compare to DBNull.Value.

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

*************************************************
Think outside of the box!
*************************************************
"Martyn Fewtrell" <mf*******@newsgroup.nospamwrote in message
news:88**********************************@microsof t.com...
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

Aug 1 '06 #3

P: n/a
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!

--
Regards

Martyn Fewtrell
"Cowboy (Gregory A. Beamer)" wrote:
Compare to DBNull.Value.

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

*************************************************
Think outside of the box!
*************************************************
"Martyn Fewtrell" <mf*******@newsgroup.nospamwrote in message
news:88**********************************@microsof t.com...
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


Aug 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.