Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys,

i want to know how to get the value of checkbox in listview and inserting to database. i got error " Object reference not set to an instance of an object."

this is my code

Dim myConnection As SqlConnection = New SqlConnection(connectionString)
        myConnection.Open()
        Dim item As ListViewDataItem
        Dim checkbox1 As CheckBox = DirectCast(ListView2.FindControl("Approve"), CheckBox)
        Dim checkbox2 As CheckBox = DirectCast(ListView2.FindControl("Dissapprove"), CheckBox)
       
        For Each item In ListView2.Items
           
            If checkbox1.Checked = True Then

                Dim c As New SqlCommand("update butiran_hantar set status=@status where depoh = '" + Label4.Text + "'", myConnection)
                c.Parameters.AddWithValue("@status", checkbox1.Text)

                Try
                    c.ExecuteNonQuery()
                    myConnection.Close()
                    BindData()
                    Label1.Text = "success."
                Catch ex As Exception
                    Label1.Text = ex.Message.ToString()
                End Try



            ElseIf checkbox2.Checked = True Then
                Dim c As New SqlCommand("update butiran_hantar set status=@status where depoh = '" + Label4.Text + "'", myConnection)
                c.Parameters.AddWithValue("@status", checkbox2.Text)

                Try
                    c.ExecuteNonQuery()
                    myConnection.Close()
                    BindData()
                    Label1.Text = "success."
                Catch ex As Exception
                    Label1.Text = ex.Message.ToString()
                End Try
            End If
           

        Next


really need help.i dont know which part is wrong.the error is at the line checkbox1.checked

regards,
musiw.
Posted
Updated 3-Dec-12 16:59pm
v2

1 solution

Hi,

My suggestion was instead using DirectCast() as your casting try to use the CType(), somebody already written some article on this
here's some example DirectCast vs. CType[^] (Im not the owner of this article)

But if its not still get the answer, maybe you can post which line the error occured when you run your program? for easy tracing here

I hope this help you on your problem

Regards,

Joven
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900