Results 1 to 6 of 6

Thread: [RESOLVED] Conversion from type 'DBNull' to type 'String' is not valid

  1. #1

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    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!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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

  4. #4
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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

  5. #5
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    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)

  6. #6

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    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!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width