VB:对象变量或者没有设置块变量 - 为什么?

时间:2014-05-19 05:01:11

标签: vb.net

我试图让我的VB应用程序写入数据库,如果选中复选框simple 1表示是,则DB中的所有行当前都设置为0。 这似乎是它指向并暗示'对象变量或With块变量未设置的代码行' 我是这个东西的全新手......

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

1 个答案:

答案 0 :(得分:2)

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

sqlComm.Parameters.AddWithValue("@cbSelect", chkBox.Checked)
相关问题