DBNull to String

  • Thread starter Thread starter Scott
  • Start date Start date
S

Scott

Hello all !

I'm working on a project which needs to connect quite frequently to a
database. When I'm tryin to retrieve data and some columns contain a
null value, my program crashes and I get the error : 'conversion from
DBNull to String is invalid'...

How can I solve this?

Thanks in advance !

Scott
 
It would be quite helpful if you actually showed us the code you use to
retrieve the column values. We can't guess what it is.

Scott said:
Hello all !

I'm working on a project which needs to connect quite frequently to a
database. When I'm tryin to retrieve data and some columns contain a
null value, my program crashes and I get the error : 'conversion from
DBNull to String is invalid'...

How can I solve this?

Thanks in advance !

Scott
 
Scott said:
Hello all !

I'm working on a project which needs to connect quite frequently to
a database. When I'm tryin to retrieve data and some columns contain
a null value, my program crashes and I get the error : 'conversion
from DBNull to String is invalid'...

How can I solve this?

Don't convert DBNull to String or replace DBNull by a String the represents
DBNull, like "<null>".


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Hello Scott

Use IsDBNull(expression) before assigning to a string
property.
Kind Regards
Jorge
 
Hi Scott,

Just as addition to Jorge you can also use

If expression Is DbNull.value then

I hope this helps?

Cor
 
Scott,
In addition to the other comments, depending on the app, I convert the NULLs
to valid values on the database, for example "" for strings & 0 for
integers. As this simplifies the VB.NET code (no need to check for NULLs in
VB.NET.

This is easiest done in your Stored Procedure or the SQL Select statement
you are using to retrieve data.

Note: I normally convert "" & 0 back to NULL in the Update/Insert Stored
Procedure, of course this means that "" & 0 are no longer valid for a
database field, which may be a problem...

Hope this helps
Jay

Scott said:
Hello all !

I'm working on a project which needs to connect quite frequently to a
database. When I'm tryin to retrieve data and some columns contain a
null value, my program crashes and I get the error : 'conversion from
DBNull to String is invalid'...

How can I solve this?

Thanks in advance !

Scott
 
Jay B. Harlow said:
Scott,
In addition to the other comments, depending on the app, I convert
the NULLs to valid values on the database, for example "" for strings
& 0 for integers. As this simplifies the VB.NET code (no need to
check for NULLs in VB.NET.

This is easiest done in your Stored Procedure or the SQL Select
statement you are using to retrieve data.

Note: I normally convert "" & 0 back to NULL in the Update/Insert
Stored Procedure, of course this means that "" & 0 are no longer
valid for a database field, which may be a problem...


Only an addition:
If DBNull and ""/0 are handled equally anyway, I'd prohibit Null values
within the database.
 
Armin,
Yes, I tend to limit NULL's also! (good point).

I should have added, when I need a NULL (such as Foreign Key references) I
will translate it on the server to something more VB.NET friendly. In the
Foreign Key case, "" & 0 would not (should not) be valid keys anyway...

Jay
 
Back
Top