472,958 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 30668
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: jim | last post by:
This line of code returns error 13, cast from 'DBNull' to type 'String' is not valid If IsDBNull(Clinics.Clinics.Item(A).Workphone) The <other code End I Clinics.Clinics is a dataset that...
5
by: DraguVaso | last post by:
Hi, Something I don't understand about a Typed DataSet: When a value in the DataSet is DBNull, it throws this error: "Cannot get value because it is DBNull". But aren't Typed DataSets...
1
by: Marty U. | last post by:
I have a form that has several fields that need to update a db record using SQL Data Provider. What is the best practice to insert DBNull if a text field is empty? I normally use if (txtbox.text...
4
by: Tina | last post by:
I have instantiated an insertRow for a dataset. I now want to make the GLCode DBNull. I have tried: insertRow.GLCode = Convert.dbnull insertRow.GLCode = Convert.dbnull(insertRow.GLCode) and...
8
by: MattB | last post by:
Hello. I have a vb.net (asp.net) application that uses ado.net datasets. At one point, I need to check a text field in a DataTable to see if there's any text in it before performing text operations...
10
by: Bob | last post by:
I'm sure there's a good reason, I just haven't been able to think of it - why didn't MS allow "DBNull" values to simply be a null (Nothing)? Bob
6
by: cj | last post by:
in the command window I get: ? result {System.DBNull} : {System.DBNull} I need to check for this in code. I then tested in the command window if result = system.DBNull 'DBNull' is a type...
2
by: Julian | last post by:
I have the following code: Dim strQuery As String = "SELECT tblData.CoPayFundGrant, tblData.M_DSS_SSA_Benefits, tblData.O_DSS_SSA_Benefits, " & _ "tblData.Ref_MPS, tblData.Ref_LA,...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
3
by: shlabadoo | last post by:
Hello, I searched for quite a while for an answer to this, but with no joy. All the answeres seem to be how to deal with getting a DBNull value out of a database, not to put it in . Perhaps I am...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.