Solved

vb.net DBNull to string error in listview

Posted on 2013-11-16
7
1,254 Views
Last Modified: 2013-11-17
I am trying to correct an error that has appeared my listview: 'Cannot convert from DBNull to string'.

However, some of the fields in the db contain null values or are empty and this is causing vb to throw the error.

I would be grateful if someone could show me how to correct this or check for null or empty values. Many thanks

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

Open in new window


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

Open in new window


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

Open in new window

0
Comment
Question by:peter-cooper
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:Gregory Miller
ID: 39653238
What line is giving you the error?

Just before that line, test for the DBNull value and just change it to an empty string or skip adding it to the listview altogether.

http://support.microsoft.com/kb/867504
0
 

Author Comment

by:peter-cooper
ID: 39653564
It is in this section of code. I need a way to check for null or empty fields. Thanks

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

Open in new window

0
 

Author Comment

by:peter-cooper
ID: 39653609
This is actually populating the listview with data.

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).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(2).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3).ToString())

            End While

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

Open in new window


But when I click on an entry it is not displaying the textboxes below.  What is the error. Thanks

Try
            While dr.Read()
                txtname.Text = dr(0)
                txtfathername.Text = dr(1)
                txtaddress.Text = dr(2)
                txtemail.Text = dr(3)
            End While
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39653648
Hi Peter;

In those columns where a value could be a NULL you need to check it before using it as shown below in the code snippet in this case using the IIF function. In that function the first parameter is the boolean test, second part is what gets executed if it is true and the last is what gets executed if it is false. Note that I am using the word NULL if the column had a null in it, but in this case it can be any valid string.

ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CStr(IIF(dr(1) IsNot DbNull.Value, dr(1), "NULL")))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CStr(IIF(dr(2) IsNot DbNull.Value, dr(2), "NULL")))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CStr(IIF(dr(3) IsNot DbNull.Value, dr(3), "NULL")))

Open in new window

0
 
LVL 11

Expert Comment

by:Gregory Miller
ID: 39653652
I do not see that you have an OnClick method created for the ListView control.
0
 

Author Closing Comment

by:peter-cooper
ID: 39654243
Thanks fernando
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39654426
Not a problem Peter, glad I was able to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question