473,715 Members | 6,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("MyValu e")

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("MyValu e") = Nothing Then

myString = DataTable.Rows( 0).Item("MyValu e")

End If

Which doesnt work!

Try/Catch just seems the wrong approach.
Regards

Martyn Fewtrell
Aug 1 '06 #1
3 13049
Martyn trying checking fo DBNull.value
Hope that helps
Patrick

"Martyn Fewtrell" <mf*******@news group.nospamwro te in message
news:88******** *************** ***********@mic rosoft.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("MyValu e")

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("MyValu e") = Nothing Then

myString = DataTable.Rows( 0).Item("MyValu e")

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*******@news group.nospamwro te in message
news:88******** *************** ***********@mic rosoft.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("MyValu e")

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("MyValu e") = Nothing Then

myString = DataTable.Rows( 0).Item("MyValu e")

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(DataT able.Rows(x).It em("MyValue")) ) Then
myString = DataTable.Rows( x).Item("MyValu e")
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*******@news group.nospamwro te in message
news:88******** *************** ***********@mic rosoft.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("MyValu e")

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("MyValu e") = Nothing Then

myString = DataTable.Rows( 0).Item("MyValu e")

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
2239
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; return NULL; }
99
5153
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
2
3404
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 how to test if this field is null. I get casting errors, and can't check for null on a null data value errors
8
4998
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 1 result to 10 results so I implemented a check to see if the array item was empty or not. The code is below based upon the code already in the python project i was handed.
10
24179
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
2498
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 && SomeObject.AnotherObject.YetAnother.HelpMePlease != null) { // Do Something }
2
6258
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 applet. Theres 10 sets of data split into 4 sub fields. The applet works fine but i'd like to make the program more robust by checking for null values. Any ideas? Thanks
42
2796
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 there is no characters in the variable it does not enter my IF statement like I would think it should. How do I properly check for a
1
2111
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 checking the column for null value using IF ( col_name IS NULL) if the column has null value ill assign the variable as null value or else ill assign the value which is there in the column another thing we can use NVL2 function to do the same...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9340
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9047
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.