473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

VB.Net - checking for Null values in DataSets

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
3 12958
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Michael B Allen | last post by:
I have a general purpose library that has a lot of checks for bad input parameters like: void * linkedlist_get(struct linkedlist *l, unsigned int idx) { if (l == NULL) { errno = EINVAL;...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
2
by: Chris | last post by:
Hi, I have an SQL Query that loads a SQLDataReader object. The returned record has bit datatype columns that I use to provide true/false values. I can't get a definite answer googling on...
8
by: Fuzzydave | last post by:
Okay, I have been handed a python project and working through it I have had to add a report. I am returning 10 variables the results of an SQL Query and as usual the number of results vary from...
10
by: mcbobin | last post by:
Hi, Here's hoping someone can help... I'm using a stored procedure to return a single row of data ie a DataRow e.g. public static DataRow GetManualDailySplits(string prmLocationID, string
13
by: Karch | last post by:
I find myself doing things like this all the time: if ( SomeObject != null && SomeObject.AnotherObject != null && SomeObject.AnotherObject.YetAnother != null &&...
2
by: misty | last post by:
First i'll introduce myself since I only joined this forum this morning so hey! <waves> Right to the problem. I'm reading values from a html file using the getParameter method to produce an...
42
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
1
by: kannan1983 | last post by:
In a table i have to check each and every column for null values . if the column has null value i need to display as ' NULL VALUE' in the output , for these iam declaring some local variables Iam...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.