asp.net gridview编辑按钮不会更新 [英] asp.net gridview edit button doesn't update

查看:177
本文介绍了asp.net gridview编辑按钮不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入新数据并按更新按钮时,它会保存旧数据(我想要更新的数据)。



When I enter new data and press update button it saves the old data (data that I want it to update).

public void fill()
{
    SqlCommand cmd = new SqlCommand("select * from school ",con );
    con.Open();
    SqlDataReader rd = cmd.ExecuteReader();

    GridView1.DataSource = rd;
    GridView1.DataBind();
    con.Close();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    fill();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    int id = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
    string  stu =((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
    int age = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);

    SqlCommand cmd = new SqlCommand("UPDATE school SET  stu_name=@stu_name,age=@age where id=@id ", con);
    cmd.Parameters.Add(new SqlParameter("@id", id));
    cmd.Parameters.Add(new SqlParameter("@stu_name", stu));
    cmd.Parameters.Add(new SqlParameter ("@age",age));

    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();

    GridView1.EditIndex = -1;
    fill();
}



问题是,分配给name,age的值是数据库中的现有值,而不是我在运行时输入的新值可以帮我??提前谢谢


he problem that the values that assigned to name,age are the existing values in the database not the new values which I entered in the runtime any one can help me?? thanks in advance

推荐答案

我已经更正了你的代码。如果你指出了发生错误的行号,我可以帮你更详细。



你错过了



I have corrected your code. I could have helped you im more detail if you have pointed out the line number where the error occurs.

You are missing

cmd.CommandType = CommandType.Text;


你的第三个代码块中的




参考此链接

http://stackoverflow.com/questions/8389803/insert-data-into-database-in-c-sharp [ ^ ]


这篇关于asp.net gridview编辑按钮不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆