1

I have a sub which just adds the values from a access db to textboxes. However, some of the fields in the db contain null values or are empty and this is causing vb to throw an error, 'Cannot convert from DBNull to string'.

How can I fix this based on my posted code or if someone could post a tutorial for this type of event, as I am a new user struggling to make sense of this. Many thanks

Sub add()
        While dr.Read()
            txtname.Text = dr(0).ToString()
            txtfathername.Text = dr(1).ToString()
            txtaddress.Text = dr(2).ToString()
            txtemail.Text = dr(3).ToString()
        End While

    End Sub

UPDATE:

Sub filllistview()
        Try
            'creatconn()
            cn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("Select * from Postings", cn)
            dr = cmd.ExecuteReader()
            While dr.Read()
                ListView1.Items.Add(dr(0).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(2))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3))

            End While

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            cn.Close()
        End Try
    End Sub

Sub showcontectsinlistview()
        Try
            'creatconn()
            cn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("select * from Postings where [Code]='" & ListView1.Text & "'", cn)
            dr = cmd.ExecuteReader()
            add()

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            cn.Close()
        End Try
    End Sub

enter image description here

user1532468
  • 1,723
  • 8
  • 41
  • 80

4 Answers4

1

I think you should just check if the values are not NULL

Sub add()
    While dr.Read()
        If dr(0) Then txtname.Text = dr(0).ToString()
        If dr(1) Then txtfathername.Text = dr(1).ToString()
        If dr(2) Then txtaddress.Text = dr(2).ToString()
        If dr(3) Then txtemail.Text = dr(3).ToString()
    End While
End Sub

There probably is a better way though...

1

Detect if the data coming from the row is empty first, then replace it with other values with will make it into a string, then you can pass it onto your textbox

Use the nothing function:

dim x as string
if dr(0).value = nothing then
    x = "Null data"
else
    x = dr(0).value
end if

It seems the problems stems on the part when you read the data from the database. can you try changing your query to not output null values?

something like

   SELECT Field
   FROM Table
   WHERE Not(Field) is null;
Malcolm Salvador
  • 1,476
  • 2
  • 21
  • 40
1

You will need to check if the value IsDBNull first like so

Dim value As String = If(dr.IsDBNull(dr(0)), "", dr(0).ToString())

then use the value

you can put this logic into a method and do the return from there.

or in your method:

Sub add()
    While dr.Read()
        If dr(0) Then txtname.Text = If(dr.IsDBNull(dr(0)), "", dr(0).ToString())
        If dr(1) Then txtfathername.Text = If(dr.IsDBNull(dr(1)), "", dr(1).ToString())
        If dr(2) Then txtaddress.Text = If(dr.IsDBNull(dr(2)), "", dr(2).ToString())
        If dr(3) Then txtemail.Text = If(dr.IsDBNull(dr(3)), "", dr(3).ToString())
    End While
End Sub

SqlDataReader.IsDBNull

also check other SO answers: SQL Data Reader - handling Null column values

.Add(dr(0).ToString())

becomes:

.Add(If(dr.IsDBNull(dr(0)), "", dr(0).ToString()))
Community
  • 1
  • 1
Ric
  • 12,855
  • 3
  • 30
  • 36
  • Ric This now causes an error in my listview, code I have placed in original question. I have posted screenshot of error. Thanks – user1532468 Nov 16 '13 at 13:09
  • Are you doing the IsDBNull check and returning an empty string instead of the null? – Ric Nov 16 '13 at 13:13
  • Ric I am using the code you posted. However, I am posting another piece of code that calls the add(). How do I know what I am returning? Thanks – user1532468 Nov 16 '13 at 13:16
  • Whenever you try to access a column value of the reader for instance dr(0) you should be doing dr.IsDBNull(dr(0)) first to check for the null – Ric Nov 16 '13 at 13:19
  • Ric How would that code in Sub filllistview() example above. Thanks – user1532468 Nov 16 '13 at 13:24
  • Ric errors with: 'index out of bounds of the array' whatever that means. – user1532468 Nov 16 '13 at 13:40
0

The shortest solution is following ...

Sub Add()
    While dr.Read()
        txtname.Text = dr(0) & ""
        txtfathername.Text = dr(1) & ""
        txtaddress.Text = dr(2) & ""
        txtemail.Text = dr(3) & ""
    End While
End Sub

This solution works also with column name !

Sub Add()
    While dr.Read()
        txtname.Text = dr("name") & ""
        txtfathername.Text = dr("father_name") & ""
        txtaddress.Text = dr("address") & ""
        txtemail.Text = dr("email") & ""
    End While
End Sub

When dr("colum_name") is DBNULL, .Text value is an empty string.

When column type is an integer, you can also initialize .Text property with "0" String.

txtNrChildren.Text = CStr(dr("nr_children") + "0")
schlebe
  • 3,387
  • 5
  • 37
  • 50