asp.net中DataGrid更新的问题

来源:百度知道 编辑:UC知道 时间:2024/05/24 05:47:41
SqlConnection ConnStr = new SqlConnection("server=localhost;database=DBNews;uid=sa;pwd=");
String StrSQL="Update Admin Set Name='"+((TextBox)e.Item.Cells[0].Controls[0]).Text+"',Password='"+ ((TextBox)e.Item.Cells[1].Controls[0]).Text+"'where Id='"+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"'";
SqlCommand cm=new SqlCommand(StrSQL,ConnStr);
ConnStr.Open();

try
{
cm.ExecuteNonQuery();
DataGrid1.EditItemIndex=-1;
Response.Write("<script>alert('更新成功!');</script>");

}
catch(SqlException)
{
Response.Write("<script>alert('更新失败!');</script>");
}
ConnStr.Close();
BindGrid();
显示的是更新成功,但是数据库中的内容没有变,也就是没有更新成功,不知道是错在什么地方了,望哪位高手帮忙指点一下,谢谢!
谢谢各位的热心帮助,还是不行啊?

你把你的sql语句换成:String StrSQL="Update Admin Set Name='"+((TextBox)e.Item.Cells[0].Controls[0]).Text+"',Password='"+ ((TextBox)e.Item.Cells[1].Controls[0]).Text+"' where Id='"+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"'";
是你的sql语句有问题了.
把我的直接复制走.你试试

你可以再SQL语句处和try以及catch处设置断点,看看SQL语句的值和到底执行到什么地方了。

楼主把你整个函数贴出来吧,看看你的形参

还有是不是少了个
finally
{
ConnStr.Close();
BindGrid();

}
呢?

可能是你在page_load方法里 读取了原始数据 如果你读取了原始数据 把数据读取部分放在if(!IsPostback){}的{}中.

你的sql语句有问题 或者你在其他地方设置了某些东西!
一楼正解,你可以断点调试下!
二楼说的原始数据应该不是!楼主说了是在数据库里面看!除非楼主没有从新打开数据库!