To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Jul 4th, 2007, 07:23 PM   #1
Alexandru_mbm
Addicted Member
 
Alexandru_mbm's Avatar
 
Join Date: Jul 07
Location: VBForums.com
Posts: 131
Alexandru_mbm is an unknown quantity at this point (<10)
Resolved [RESOLVED] Conversion from type 'DBNull' to type 'String' is not valid

Hello to you all,

I have searched almost the all forum to resolve my problem but without any positive effect.

I have a problem reading from the database some values.

If one of the value is empty i get this error message:
Conversion from type 'DBNull' to type 'String' is not valid.

How can i pass thru this problem ?

Please give me a suggestion and how can i optimize this code better!

Code:
Dim denumire_furnizor, tip_unitate, statut_juridic As String

        conn.Open()

        Dim sql_1 = "SELECT denumire FROM unitatea_economica"
        cmd = New OleDb.OleDbCommand(sql_1, conn)
        denumire_furnizor = CStr(cmd.ExecuteScalar)

        Dim sql_1_1 = "SELECT tip_unitate FROM unitatea_economica"
        cmd = New OleDb.OleDbCommand(sql_1_1, conn)
        tip_unitate = CStr(cmd.ExecuteScalar)

        Dim sql_1_2 = "SELECT statut_juridic FROM unitatea_economica"
        cmd = New OleDb.OleDbCommand(sql_1_2, conn)
        statut_juridic = CStr(cmd.ExecuteScalar)

        Me.lbl_furnizor.Text = tip_unitate & " " & denumire_furnizor & " " & statut_juridic

        If conn.State <> ConnectionState.Closed Then

            conn.Close()

        End If
Thanks alot and please to escuse me for my bad english!
Alexandru_mbm is offline   Reply With Quote
Old Jul 4th, 2007, 07:48 PM   #2
Shaggy Hiker
Loquacious User
 
Shaggy Hiker's Avatar
 
Join Date: Aug 02
Location: Idaho
Posts: 11,014
Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)
Re: Conversion from type 'DBNull' to type 'String' is not valid

The queries you have written will return just the first value from the table you are querying (if it is a table), which seems a bit peculiar. Usually there's a WHERE clause in there to reduce the number of records, but that's not the cause of the problem. The problem comes from the fact that one or all of these fields have a Null value in the first record.

I can't say that I have ever tried to check for Null on an ExecuteScalar call before, but I would think....that it would be a pain. If you simply check whether cmd.ExecuteScalar returns a Null, that would require two calls. Better would be to put the data into an object and test the object. However, if you have access to the database, and control over the design of it, the best solution would be to not have Null (empty) values in those fields. Give them a default value of pretty much anything. I think even an empty string ("") would do.
__________________
My usual boring signature: Nothing
Shaggy Hiker is offline   Reply With Quote
Old Jul 5th, 2007, 12:27 PM   #3
Al42
PowerPoster
 
Join Date: Feb 06
Location: East of NYC, USA
Posts: 5,692
Al42 is a jewel in the rough (300+)Al42 is a jewel in the rough (300+)Al42 is a jewel in the rough (300+)Al42 is a jewel in the rough (300+)
Re: Conversion from type 'DBNull' to type 'String' is not valid

Code:
denumire_furnizor = CStr("" & cmd.ExecuteScalar)
should work.
__________________
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.

Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

Please Help Us To Save Ana
Al42 is offline   Reply With Quote
Old Jul 5th, 2007, 01:00 PM   #4
bmahler
Frenzied Member
 
bmahler's Avatar
 
Join Date: Oct 05
Location: Somewhere just west of the Atlantic
Posts: 1,564
bmahler is a jewel in the rough (300+)bmahler is a jewel in the rough (300+)bmahler is a jewel in the rough (300+)bmahler is a jewel in the rough (300+)
Re: Conversion from type 'DBNull' to type 'String' is not valid

I ran into the same issue and wrote a little function
Code:
    Public Function FixNull(ByVal o As Object) As Object
        If IsDBNull(o) Then
            Return Nothing
        Else
            Return o
        End If
    End Function
Then you can just write
Code:
denumire_furnizor = FixNull(cmd.ExecuteScalar)
__________________
Boooya
  • Visual Studio 2008 Professional
  • Don't forget to use [CODE]your code here[/CODE] when posting code
  • Don't forget to rate helpful posts!
  • If you're question was answered please mark your thread [Resolved]

Code Contributions:
PHP
PHP Image Gallery v1.0PHP Image Gallery v2.0
VB 2005
Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


Useful Links: (more to come)
MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds
bmahler is offline   Reply With Quote
Old Jul 5th, 2007, 01:28 PM   #5
bgmacaw
Fanatic Member
 
bgmacaw's Avatar
 
Join Date: Mar 07
Location: Atlanta, GA USA
Posts: 524
bgmacaw is a jewel in the rough (200+)bgmacaw is a jewel in the rough (200+)bgmacaw is a jewel in the rough (200+)
Re: Conversion from type 'DBNull' to type 'String' is not valid

Use String.Concat


vb Code:
  1. lbl_furnizor.Text = String.Concat(tip_unitate, " ", denumire_furnizor, " ", statut_juridic)
__________________
| My VB Notebook For .NET | My VB6 Notebook |
bgmacaw is offline   Reply With Quote
Old Jul 5th, 2007, 07:17 PM   #6
Alexandru_mbm
Addicted Member
 
Alexandru_mbm's Avatar
 
Join Date: Jul 07
Location: VBForums.com
Posts: 131
Alexandru_mbm is an unknown quantity at this point (<10)
Re: Conversion from type 'DBNull' to type 'String' is not valid

Many thanks to all of you!!!

Shaggy Hiker

Yes it's a table with only one row.
Thank you for the indications!

My problem is now solved...

I have used "bmahler" solution. I dont know if it is the best one but it works and seems to be more simple.

Many thanks to all of you again!!!
Alexandru_mbm is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:33 PM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.