简体   繁体   English

VB:未设置对象变量或With块变量-为什么?

[英]VB: Object variable or With block variable not set - why?

I am trying to get my VB app to write to a database if a check box is selected simple 1 to indicate yes, all rows in the DB are currently set to 0. THis seems to be the line of code that its pointing to and suggesting that 'Object variable or With block variable not set' I am a complete newbie with this thing... 我试图让我的VB应用程序将数据库写入数据库,如果选中了一个简单的复选框以表示是,则数据库中的所有行当前都设置为0。这似乎是其指向并建议的代码行那个“对象变量或没有设置块变量”,我是这个东西的新手。

   sqlComm.Parameters.AddWithValue("@cbSelect", cbSelect.Checked) 'passing the @chkBox parameter to the command

here is the code behind. 这是背后的代码。

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

here is the ASPX: 这是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" />

Well, looking at the code here you seem to be trying to call for a property of an object you're not instantiating. 好吧,看一下这里的代码,您似乎正在尝试调用未实例化的对象的属性。 I believe in your code you mean to be calling 我相信您的代码意味着您要致电

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM