VB:SQL更新似乎没有更新记录

时间:2014-05-19 06:30:38

标签: asp.net sql vb.net

我在gridview中有一个复选框,当勾选时我需要它将记录从0更新为1.我有下面的代码,它只是没有更新任何记录。没有错误抛出,所以我不确定它在做什么...

这是背后的代码。

   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=XXXXXXX\XXXXX;Initial Catalog=Support_Metrics;Persist Security Info=True;User ID=reportserver;Password=XXXXXXXX")
        Dim sqlComm As New SqlCommand("update BusinessSignups set contacted=@cbSelect where ID='" & gvRow.Cells(0).Text & "'", sqlcon) 'this is an insert example, you can do update you can get the current gridView row id using gvRow.Cells(0).Text
        'Dim sqlComm As New SqlCommand("insert into BusinessSignups (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", chkBox.Checked) 'passing the @cbSelect parameter to the command
        'sqlComm.Parameters.Add("@ID", System.Data.SqlDbType.Int)
        Using (sqlcon)
            sqlcon.Open() 'open connection
            sqlComm.ExecuteNonQuery() 'execute the command
        End Using
    Next
End Sub 

以下是一些ASPX代码:

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" AutoGenerateEditButton="True" CellPadding="4" DataSourceID="BusinessSignUps" ForeColor="#333333" GridLines="None" DataKeyNames="ID">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ShowSelectButton="False" />
            <asp:TemplateField>
                    <ItemTemplate>
                         <asp:CheckBox ID="cbSelect" runat="server" Checked="false" />
                    </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="ID" Visible="true" />
.....

我哪里错了?请放轻松,这对我来说都是新的。

1 个答案:

答案 0 :(得分:0)

在我的sql更新或插入后,我经常忘记做的一件事是commit;。这有助于SqlConnection自动提交还是自动提交?