468,490 Members | 2,545 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

DBNull to String

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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #1
7 30019
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" <ni**********@pandora-dot-be.no-spam.invalid> wrote in message
news:40********@Usenet.com...
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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 20 '05 #2
Hello Scott

Use IsDBNull(expression) before assigning to a string
property.
Kind Regards
Jorge
-----Original Message-----
Hello all !

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

How can I solve this?

Thanks in advance !

Scott
Posted Via Usenet.com Premium Usenet Newsgroup Services
--------------------------------------------------------- - ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
--------------------------------------------------------- - http://www.usenet.com
.

Nov 20 '05 #3
"Scott" <ni**********@pandora-dot-be.no-spam.invalid> schrieb
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

Nov 20 '05 #4
Cor
Hi Scott,

Just as addition to Jorge you can also use

If expression Is DbNull.value then

I hope this helps?

Cor
Nov 20 '05 #5
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" <ni**********@pandora-dot-be.no-spam.invalid> wrote in message
news:40********@Usenet.com...
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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 20 '05 #6
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
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

Nov 20 '05 #7
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
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

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Marty U. | last post: by
4 posts views Thread by Tina | last post: by
8 posts views Thread by MattB | last post: by
10 posts views Thread by Bob | last post: by
6 posts views Thread by cj | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.