1
我有一個帶有複選框的數據網格,它調用一個名爲checkbox_CheckedChanged的例程。到現在爲止還挺好。我設法弄清楚了dataview中另一列的值,這使我能夠確定我正在處理的行的id。vb.net SQL查詢在SQL服務器中工作,但在複選框中調用時不起作用
我試圖讓它改變定義複選框的初始值的列的值,但是我寫的SQL在vb.net調用時不起作用 - 它在輸入時工作然而,手動到SQL服務器。
這裏是我的背後代碼:
Public Sub checkbox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) 'Handles checkbox.CheckedChanged
Dim connectionString As String = WebConfigurationManager.ConnectionStrings("edinsec").ConnectionString
Dim box As CheckBox = DirectCast(sender, CheckBox)
Dim tblcell As TableCell = CType(box.Parent, TableCell)
Dim dgRow As GridViewRow = CType(tblcell.Parent, GridViewRow)
Dim msgId As Integer = unreadMessages.Rows(dgRow.DataItemIndex).Cells(0).Text
Dim insertSQL As String
If box.Checked = True Then
insertSQL = "UPDATE messages"
insertSQL &= "SET readit = 0"
insertSQL &= "WHERE msgid = @msgId"
Else
insertSQL = "UPDATE messages"
insertSQL &= "SET readit = 1"
insertSQL &= "WHERE msgid = @msgId"
End If
Using con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(insertSQL, con)
cmd.Parameters.AddWithValue("@msgId", msgId)
Try
con.Open()
cmd.ExecuteNonQuery()
Catch Err As SqlException
MsgBox("Error", 65584, "Insertion Error")
End Try
con.Close()
End Using
End Sub
的想法是,點擊後,該複選框將在數據庫中,以它的反面翻轉「readit的價值。它不斷跳轉到SqlException,所以我看到了錯誤消息。
的複選框的ASPX代碼是這樣的:
<asp:TemplateField HeaderText="readit" SortExpression="readit">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox_CheckedChanged" Checked='<%# Bind("readit") %>'
Enabled="true" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox_CheckedChanged" Checked='<%# Bind("readit") %>' />
</EditItemTemplate>
</asp:TemplateField>
任何幫助將非常感激。 Visual Studio(2008)給出了零在這種情況下對SQL錯誤的反饋,這非常令人生氣。
克勞斯 - 非常感謝,這使得SQL運行。麻煩的是,我必須點擊複選框兩次才能更改爲「粘貼」...編輯:我只是有邏輯錯誤的方式... noob錯誤:) – melat0nin 2010-11-22 10:53:40