更新数据出错
我在daragrid里面更新数据的时候有错误,一样的方法做了两个页,但是一个页是可以正常运行,另一个却不可以!~运行之后,不出任何的错误,但是更改了数据之后,数据库不产生任何变化。。代码如下:
string content;
int id;
DropDownList deep = (DropDownList)e.Item.FindControl( "DropDownList1 ");
DropDownList answer = (DropDownList)e.Item.FindControl( "DropDownList2 ");
id = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
content = ((TextBox)e.Item.FindControl( "TextBox1 ")).Text;
SqlConnection conn;
SqlCommand cmd;
conn = new SqlConnection(ConfigurationSettings.AppSettings[ "constring "]);
conn.Open();
cmd = new SqlCommand( "update panduan content=@content,deep=@deep;answer=@answer where id=@id ",conn);
cmd.Parameters.Add( "@id ",id);
cmd.Parameters.Add( "@content ",content);
cmd.Parameters.Add( "@deep ",deep.SelectedValue);
cmd.Parameters.Add( "@answer ",answer.SelectedValue);
cmd.ExecuteNonQuery();
conn.Close();
Response.Write(@ " <script language= 'Javascript '> alert( '更新成功! ') </script> ");
DataGrid1.EditItemIndex = -1;
data();
[解决办法]
cmd.Parameters.Add( "@id ",id);放到后面看看
最好用trace跟踪sql
[解决办法]
cmd = new SqlCommand( "update panduan set content=@c,deep=@d,answer=@a where id=@id ",conn);
cmd.Parameters.Add( "@c ",C);
cmd.Parameters.Add( "@d ",D.SelectedValue);
cmd.Parameters.Add( "@a ",A.SelectedValue);
cmd.Parameters.Add( "@id ",id);
参数传值!问题吧!一般上注意顺序!