Null value in Datareader

  • Thread starter Thread starter axapta
  • Start date Start date
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
 
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.
 
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.
 
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]
 
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.
 
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]
 
Back
Top