【问题标题】:VB: Object variable or With block variable not set - why?VB:对象变量或未设置块变量 - 为什么?
【发布时间】: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


【解决方案1】:

好吧,看看这里的代码,您似乎在尝试调用您未实例化的对象的属性。我相信你的代码你的意思是打电话

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多