【发布时间】:2014-05-19 05:01:11
【问题描述】:
如果选择了一个复选框,我正在尝试让我的 VB 应用程序写入数据库,简单 1 表示是,数据库中的所有行当前都设置为 0。 这似乎是它指向并建议“对象变量或未设置块变量”的代码行 我是这个东西的新手......
sqlComm.Parameters.AddWithValue("@cbSelect", cbSelect.Checked) 'passing the @chkBox parameter to the command
这是后面的代码。
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
For Each gvRow As GridViewRow In GridView1.Rows 'itterate tru all rows
Dim chkBox As CheckBox = CType(gvRow.FindControl("cbSelect"), CheckBox) 'find the checkBox inside GridView
Dim sqlcon As New SqlConnection("Data Source=SYD-PB0FW9M\blah;Initial Catalog=Support_Metrics;Persist Security Info=True;User ID=reportserver;Password=xxxxxxxx")
Dim sqlComm As New SqlCommand("insert into contacted values (@cbSelect)", sqlcon) 'this is an insert example, you can do update you can get the current gridView row id using gvRow.Cells(0).Text
sqlComm.Parameters.AddWithValue("@cbSelect", cbSelect.Checked) 'passing the @cbSelect parameter to the command
Using (sqlcon)
sqlcon.Open() 'open connection
sqlComm.ExecuteNonQuery() 'execute the command
End Using
Next
End Sub
这是 ASPX:
<Columns>
<asp:CommandField ShowSelectButton="False" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" Checked="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="account_id" HeaderText="account_id" SortExpression="account_id" />
【问题讨论】:
-
什么是
cbSelect?你的意思是chkBox代替吗? -
使用更多上下文进行编辑 - 没错,这就是 ID。
-
好吧,看看这里的代码,您似乎试图调用您未实例化的对象的属性。我相信你的代码,你的意思是打电话给
sqlComm.Parameters.AddWithValue("@cbSelect", chkBox.Checked) -
阿德里安做得很好。谢谢
-
好吧,既然这是正确答案,我将其添加为答案。
标签: vb.net