?
Solved

vb.net DBNull to string error in listview

Posted on 2013-11-16
7
Medium Priority
?
1,389 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 12

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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 12

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 64

Expert Comment

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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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