编辑gridview后无法更新对数据库的更改.没有抛出错误,但是数据库中的表中没有更新更改

本文关键字:数据库 更新 gridview 错误 编辑 出错 | 更新日期: 2023-09-27 17:50:59

web config

<add name="MyConnection" connectionString="Data Source=HABIBDEA-PC;Initial Catalog=kopma;Persist Security Info=True;User ID=sa;Password=ilovedea1" />
<add name="kopmaConnectionString" connectionString="Data Source=HABIBDEA-PC;Initial Catalog=kopma;User ID=sa;Password=ilovedea1"
  providerName="System.Data.SqlClient" />
<add name="kopmaConnectionString2" connectionString="Data Source=HABIBDEA-PC;Initial Catalog=kopma;Persist Security Info=True;User ID=sa;Password=ilovedea1"
  providerName="System.Data.SqlClient" />

1。这是我要更新的代码。

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
 TextBox nim = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtnime");
 TextBox status = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtstat");
 con.Open();
 SqlCommand cmd = new SqlCommand("update datauser set STATUSANGGOTA=@STATUSANGGOTA where NIM=@NIM", con);
            cmd.Parameters.AddWithValue("@STATUSANGGOTA",status.Text.ToString());
            cmd.Parameters.AddWithValue("@NIM", nim.Text.ToString());
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            con.Close();          
            GridView1.EditIndex = -1;
            gvbind();                 
}

2。这是我的连接字符串

public partial class UserAktif : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ToString());
    String s = "AKTIF";
    protected void Page_Load(object sender, EventArgs e)
    {
        gvbind();
    }

my aspx code

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="NIM" CssClass="table table-striped table-bordered bootstrap-datatable datatable responsive" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing" AllowPaging="True" PageSize="5" OnRowUpdating="GridView1_RowUpdating">
                    <Columns>
                        <asp:TemplateField HeaderText="NIM">
                            <ItemTemplate>
                                <asp:Label ID="lblnim" runat="server" Text='<%# Eval("NIM") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtnime" runat="server" Text='<%# Eval("NIM")%>' Enabled="false"></asp:TextBox>
                            </EditItemTemplate>
                                  </asp:TemplateField>     
                        <asp:TemplateField HeaderText="Status">
                            <ItemTemplate>
                                <asp:Label ID="lblstat" runat="server" Text='<%# Eval("STATUSANGGOTA") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtstat" runat="server" Text='<%# Eval("STATUSANGGOTA") %>' >                                     
                                </asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Action">
                            <ItemTemplate>
                                <asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" CssClass="btn btn-info">Edit</asp:LinkButton><br />                      
                            </ItemTemplate>
                            <EditItemTemplate>
                                 <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" CssClass="btn btn-success">Update</asp:LinkButton>
                                <br />
                                <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" CssClass="btn btn-danger">Cancel</asp:LinkButton>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>             

可以帮我吗?如果我点击更新,没有错误,但没有更改数据库和gridview, tolong bisa gaa, kerjaan KP inh - -

编辑gridview后无法更新对数据库的更改.没有抛出错误,但是数据库中的表中没有更新更改

最后,我使用默认的gridview更新命令我用这个,完美的工作^^

    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:kopmaConnectionString %>" SelectCommand="SELECT [NIM], [STATUSANGGOTA] FROM [datauser] WHERE ([STATUSANGGOTA] = @STATUSANGGOTA)" 
    UpdateCommand="UPDATE datauser SET [STATUSANGGOTA]=@STATUSANGGOTA WHERE [NIM]=@NIM">
        <SelectParameters>
            <asp:Parameter DefaultValue="AKTIF" Name="STATUSANGGOTA" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>