Results 1 to 4 of 4

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

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    58

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

    Dear Experts
    I use following codes to retrieve data from table, but get error

    Conversion from type 'DBNull' to type 'String' is not valid.
    Code:
    str = "Select * from gpass where vou_no= " & Val(Me.txtGat.Text) & " And Date =   '" & Me.txtDat.Text & "'"
            dt = GetTable(str)
    
            If (dt.Rows.Count > 0) Then
    
    
                DataGridView1.Rows.Clear()
    
                For i = 0 To dt.Rows.Count - 1
    
                    Me.DataGridView1.Rows.Add()
                    Me.DataGridView1.Rows(i).Cells(1).Value = IIf(IsDBNull(dt.Rows(i)("tittle")), " ", Trim(dt.Rows(i)("tittle")))
                Next
            End If
    End If
    Tittle column of gpass is as

    Column------datatype
    Tittle--------char(100)

    And data in column is as
    Null
    Null
    Null


    Please help

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Conversion from type 'DBNull' to type 'String' is not valid

    In VB 2008 you should be using If, not IIf. One of the problems with IIf is that both possible values get evaluated, so you're trying to Trim a DBNull value even if you don't intend to use it. The new If operator short-circuits, meaning that only the expression for the value that will be returned is evaluated.

    That said, why do you need to any of that? Why can't you just do this:
    vb.net Code:
    1. Me.DataGridView1.Rows(i).Cells(1).Value = dt.Rows(i)("tittle")
    A null value will be displayed in exactly the same way as an empty string in that case, so there's no need to check. If the problem is that you want to Trim a non-empty string then you can do this:
    vb.net Code:
    1. Me.DataGridView1.Rows(i).Cells(1).Value = dt.Rows(i)("tittle").ToString().Trim()
    By the way, assuming it's English, there's no such word as "tittle". That should probably be "title".

    Finally, you should also be using parameters to insert values into your SQL code rather than string concatenation. Follow the Database FAQ link in my signature for more information on that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

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


  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    Quote Originally Posted by nbrege View Post
    Hmmm... Never say never I guess. Given the context though, I'm going with "title" on this one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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