如何获取listview中复选框的值? [英] how to get the value of checkbox in listview?

查看:64
本文介绍了如何获取listview中复选框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想知道如何在listview中获取checkbox的值并插入数据库。我收到错误对象引用没有设置为对象的实例。



这是我的代码



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





真的需要帮助。我不知道哪个部分是错的。错误就行了.check1.checked



问候,

musiw。



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

regards,
musiw.

推荐答案





我的建议是使用DirectCast()作为你的演员尝试使用CType(),有人已经在这上面写了一些文章

这里有一些例子DirectCast与CType [ ^ ](我不是这篇文章的拥有者)



但是如果它还没有得到答案,也许您可​​以发布运行程序时出现错误的哪一行?在这里轻松追踪



我希望这可以帮助你解决问题



问候,



Joven
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


这篇关于如何获取listview中复选框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆