1
我試圖讓我的VB應用程序寫入數據庫,如果複選框被選中,簡單1表示是,數據庫中的所有行都設置爲0. 這似乎是它的代碼行並提示「對象變量或帶塊變量未設置」 我是一個完整的新手這個東西......VB:對象變量或塊變量未設置 - 爲什麼?
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'代替嗎? – Abhitalks
用更多的上下文做了編輯 - 是正確的,這是ID。 – user2034164
那麼,看看這裏的代碼,你似乎試圖調用一個你沒有實例化的對象的屬性。我相信你的代碼你的意思是調用'sqlComm.Parameters.AddWithValue(「@ cbSelect」,chkBox.Checked)' – Adrian