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

Null Database Values

P: n/a
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so
here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is
not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute ""
where necessary?

Thanks,
Dan
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Dan
I check to see if the value is null before assiging. In my case with the
Reader object

If Not(rdr.IsDBNull(1)) Then myvar = rdr.GetString(1)
"Dan Keeley" <ma********@hotmail.com> wrote in message
news:8J*****************@newsfep4-winn.server.ntli.net...
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so
here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is
not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute "" where necessary?

Thanks,
Dan

Nov 20 '05 #2

P: n/a
You need to check for null first before assigning:

if not currentRow("FaxNumber") = DBNull.Value

end if

"Dan Keeley" <ma********@hotmail.com> wrote in message
news:8J*****************@newsfep4-winn.server.ntli.net...
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so
here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is
not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute "" where necessary?

Thanks,
Dan

Nov 20 '05 #3

P: n/a
Compare the value to System.DBNull.Value:

If Not (your reference to the possible null) Is DBNull.Value Then
.....
'Assign

Else
....
'Handle the null case for example:
myVal = String.Empty
End if
--

Justin Weinberg
Designing a PrintDocument? Drawing to forms?
Check out GDI+ Architect at www.mrgsoft.com

"Dan Keeley" <ma********@hotmail.com> wrote in message
news:8J*****************@newsfep4-winn.server.ntli.net...
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so
here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is
not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute "" where necessary?

Thanks,
Dan

Nov 20 '05 #4

P: n/a
"Dan Keeley" <ma********@hotmail.com> schrieb
This is probably not a dotnet specific issue, however i'm using
dotnet so here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type
String is not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and
substitute "" where necessary?

You could write a function to do it:

Function MyFunction(ByVal o As Object) As String
If o Is DBNull.Value Then
Return ""
Else
Return o.ToString
End If
End Function
As you want to substitue Null by a zero-length string, how do you
distinguish between Null and a zero-length string when you look at the
filled textbox?

--
Armin

http://learn.to/quote
http://www.plig.net/nnq/nquote.html

Nov 20 '05 #5

P: n/a

Ok thanks for the replys, i'll probably go the adding a function route.

Rgds,
Dan

"Justin Weinberg" <jweinberg@_spamkill_mrgsoft.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
Compare the value to System.DBNull.Value:

If Not (your reference to the possible null) Is DBNull.Value Then
....
'Assign

Else
...
'Handle the null case for example:
myVal = String.Empty
End if
--

Justin Weinberg
Designing a PrintDocument? Drawing to forms?
Check out GDI+ Architect at www.mrgsoft.com

"Dan Keeley" <ma********@hotmail.com> wrote in message
news:8J*****************@newsfep4-winn.server.ntli.net...
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute

""
where necessary?

Thanks,
Dan


Nov 20 '05 #6

P: n/a
I ran into this problem early on, so I ended up just adding a Check/Select
function pair for each data type. I know it's not the most efficient coding
method, but it works. Each Select function selects the value into an object
and returns the value of the Check function, which converts the object to
its specific data type, or returns 0 for numerics and "" for strings. Then
just put all of those functions in a global Query class or something along
those lines, and you can always access them easily.

Mike
Michael Caputo
Programmer/Database Administrator
Simon Economic Systems Ltd.

"Dan Keeley" <ma********@hotmail.com> wrote in message
news:8J*****************@newsfep4-winn.server.ntli.net...
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so
here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is
not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute "" where necessary?

Thanks,
Dan

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.