日期:2014-05-18  浏览次数:20855 次

请教GridView更新不到数据?谢谢
我在GridView里绑定了一个TextBox2,但更改TextBox2里的内容时,按RowUpdating更新不了数据??
((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;这一句有什么错误??

------------------------------语句如下:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  { 

  string m_name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
  con.Open();
  string cmdtext = "update back set m_name='"
  + m_name + "' where m_id='"
  + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
  SqlCommand cmd = new SqlCommand(cmdtext, con);
  cmd.ExecuteNonQuery();  
  }

------解决方案--------------------
添加一个判断行,
e.rowIndex>-1
{
方法里的代码
}
------解决方案--------------------
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if(e.rowIndex>-1)
{
string m_name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
con.Open();
string cmdtext = "update back set m_name='"
+ m_name + "' where m_id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand cmd = new SqlCommand(cmdtext, con);
cmd.ExecuteNonQuery(); 
}
}

因为我以前也遇到这样的问题.
------解决方案--------------------
唉.我没区分大小写...你自己在e后面....一下自己把rowindex打出来
------解决方案--------------------
给你的cmd.ExecuteNonQuery(); 赋一个值
int result = cmd.ExecuteNonQuery(); 
然后看看有没有得到值,如果得到的值为-1,就说明你没有更新成功,也许是你的sql语句有错误!
------解决方案--------------------
探讨
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if(e.rowIndex>-1)
{
string m_name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
con.Open();
string cmdtext = "update back s…

------解决方案--------------------
你都没打开数据库,怎么去更新?
SqlCommand cmd = new SqlCommand(cmdtext, con); 
cmd.connection.Open();//打开
cmd.ExecuteNonQuery();
cmd.connection.Close();//关闭
------解决方案--------------------
在page_load中你是不是把页面刷新了,所以把值冲掉了。加个!isposback看看