Null value in Datareader

A

axapta

Hi Group,
How can I overcome the issue of null values in a datareader. I keep getting
the dbnull error when I try to assign a null value to a text box.

TIA
 
Z

zacks

Hi Group,
How can I overcome the issue of null values in a datareader.  I keep getting
the dbnull error when I try to assign a null value to a text box.

TIA

Check out the DataReader IsDBNull Method.
 
S

SurturZ

You need to test each and every field in your dataset if it is Null before
using it.

This annoyed me so much that I now just make all my database fields
non-nullable.

A common solution is to have a "NoNull" function or something that
automatically changes nulls to zero or empty string.
 
R

rowe_newsgroups

Hi Group,
How can I overcome the issue of null values in a datareader. I keep getting
the dbnull error when I try to assign a null value to a text box.

TIA

It's actually quite easy, and requires no checks for DbNull if you
going to a string value:

//////////////
If dataReader.Read() Then
textBox1.Text = dr("MyColumnName").ToString()
End If
/////////////

The ToString() method will happily turn a DbNull into a blank string
("") when called. It's a great time saver :)

Thanks,

Seth Rowe [MVP]
 
C

cfps.Christian

I always had to do a check on my datareader values as they came out of
the database.

if not dr[0] is DBNull.Value then
textbox1.text = dr[0].ToString() 'I always have option strict on
end if

Kind of a pain, which is why I stopped using null unless I had to.
 
R

rowe_newsgroups

I always had to do a check on my datareader values as they came out of
the database.

if not dr[0] is DBNull.Value then
textbox1.text = dr[0].ToString() 'I always have option strict on
end if

Kind of a pain, which is why I stopped using null unless I had to.

You should be able to just do "textbox1.text = dr[0].ToString()" since
as written above the ToString will convert a dbnull to a blank string.

Thanks,

Seth Rowe [MVP]
 

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

Top