Vb.net VB:未设置对象变量或带块变量-为什么?

Vb.net VB:未设置对象变量或带块变量-为什么?,vb.net,Vb.net,我正在尝试让我的VB应用程序写入数据库如果选中复选框simple 1表示是,则DB中的所有行当前都设置为0。 这似乎是它指向的代码行,并建议“未设置对象变量或With block variable” 我对这件事完全是个新手 sqlComm.Parameters.AddWithValue("@cbSelect", cbSelect.Checked) 'passing the @chkBox parameter to the command 这是背后的代码 Protected Sub But

我正在尝试让我的VB应用程序写入数据库如果选中复选框simple 1表示是,则DB中的所有行当前都设置为0。 这似乎是它指向的代码行,并建议“未设置对象变量或With block variable” 我对这件事完全是个新手

   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" />


好吧,看看这里的代码,你似乎试图调用一个你没有实例化的对象的属性。我相信你的密码你想打电话给我

sqlComm.Parameters.AddWithValue("@cbSelect", chkBox.Checked)

什么是
cbSelect
?你的意思是代替chkBox吗?使用更多上下文进行编辑-这是正确的ID。好吧,看看这里的代码,你似乎试图调用一个你没有实例化的对象的属性。我相信你的代码,你想调用
sqlComm.Parameters.AddWithValue(“@cbSelect”,chkBox.Checked)
Adrian做得很好。谢谢好吧,既然那是正确的答案,我就加上它作为答案。