【问题标题】:Edit Sql Database Using Gridview Asp.net c#使用 Gridview Asp.net c# 编辑 Sql 数据库
【发布时间】:2014-04-14 16:26:41
【问题描述】:

我正在尝试在 c# 中使用 gridview 编辑学生表的信息 但是我的代码在不更改我的 sql 数据库中的表的情况下更新了 gridview 不知道问题出在哪里
这是我的网格视图:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
              DataKeyNames="S_ID"  OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AllowPaging="true" >
              <Columns>

              <asp:TemplateField HeaderText="ID">
                    <EditItemTemplate>
                        <asp:TextBox ID="S_ID" runat="server" Text='<%# Eval("S_ID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("S_ID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="S_Name" runat="server" Text='<%# Eval("S_Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("S_Name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Email">
                    <EditItemTemplate>
                        <asp:TextBox ID="S_Email" runat="server" Text='<%# Eval("S_Email") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="S_Email" runat="server" Text='<%# Bind("S_Email") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                  <asp:CommandField ShowEditButton="True" />
              </Columns>
          </asp:GridView>

这是背后的代码:

protected void gvbind()
    {
        SqlConnection conn = new SqlConnection(@"MYCONNECTION");
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT S_ID , S_Name , S_Email , B_ID FROM Student", conn);

        DataTable dt = new DataTable();
        dt.Load(cmd.ExecuteReader());
        GridView1.DataSource = dt; // now assign this datatable dt to gridview as datasource
        GridView1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            gvbind();
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        gvbind();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {


        int sUserId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("S_Name"); //give the edititem template ID
        TextBox email = (TextBox)GridView1.Rows[e.RowIndex].FindControl("S_Email");
        try
        {
            SqlConnection conn = new SqlConnection(@"MYCONNECTION");
            conn.Open();
            SqlCommand c = new SqlCommand("Update Student set S_Name='" + name.Text + "',S_Email='" + email.Text + "'where S_ID='" + sUserId + "'");
            c.Connection = conn;
            c.ExecuteNonQuery();

            GridView1.EditIndex = -1;
            gvbind();
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterStartupScript(typeof(string), "Key", "alert('Error : \\n" + ex.Message + "');", true);
        }
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        gvbind();
    }

please can anyone help me why this change the gridview only but not the DB ?!

【问题讨论】:

    标签: c# asp.net sql-server gridview edit


    【解决方案1】:

    我认为我在您的代码中看到的第一个问题是:

    SqlCommand c = new SqlCommand("Update Student set S_Name='" + name.Text + "',S_Email='" + email.Text + "'where S_ID='" + sUserId + "'");
    

    如果您数据库中的S_ID 列是Integer,那么您不应该使用' 字符 缩进参数sUserId 它只是:

    where S_ID = " + sUserId +")"
    

    【讨论】:

    • 或者更好:使用参数!
    • SqlCommand c = new SqlCommand("更新学生集 S_Name= @s ,S_Email= @e where S_ID = @id"); c.连接=连接; c.Parameters.AddWithValue("@s", name.Text); c.Parameters.AddWithValue("@e", email.Text); c.Parameters.AddWithValue("@id", sUserId); c.ExecuteNonQuery();注意改变:(
    • @marc_s 请查看我的评论
    • 我很久以前就遇到过这个问题,但我忘记了我是如何解决的,但我的建议是你应该使用 CommandName 和 commandAgrument
    • 尝试使用这个:e.NewValues["S_Name"] = name.text();e.NewValues["S_Email"] = email.text();然后字符串名称 = e.NewValues["S_Name"].ToString();字符串电子邮件 = e.NewValues["S_Email"].ToString();
    猜你喜欢
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多