请教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语句有错误!
[解决办法]