求助!DataGrid控件的更新列怎么更新不了数据?
各位朋友帮忙看看我的代码错在哪里?我在DataGrid控件里加了个编辑按钮列,可是运行后,数据更新不到数据库中?是什么原因阿?我是菜鸟,看拉好久没找到错误原因。
private void LoadData()//将DataGrid控件邦定到数据源
{
SqlDataAdapter sqlDa=new SqlDataAdapter();
SqlConnection conn=new SqlConnection("Server=.;uid=sa;password=sa;Database=zra");
conn.Open();
SqlCommand selectCmd=new SqlCommand("select * from getixinxi");
selectCmd.Connection=conn;
sqlDa.SelectCommand=selectCmd;
DataSet ds=new DataSet();
sqlDa.Fill(ds,"getixinxi");
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
conn.Close();
conn.Dispose();
selectCmd.Dispose();
}
private void DataGrid1_UpdateCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlCommand cmd=new SqlCommand();
cmd.Connection=new SqlConnection("Server=.;uid=sa;password=sa;Database=zra");
string dengjimingcheng = this.DataGrid1.DataKeys[0].ToString();
getixinxi set"+" jingyingfanwei=@jingyingfanwei,jingyingdizhi=@jingyingdizhi,zhucezhijin=@zhucezhijin where dengjimingcheng=@dengjimingcheng";//更新语句
SqlParameter prmjingyingfanwei=new SqlParameter("@jingyingfanwei",SqlDbType.Char,10);
prmjingyingfanwei.Direction=ParameterDirection.Input;
cmd.Parameters.Add(prmjingyingfanwei);
SqlParameter prmjingyingdizhi=new SqlParameter("@jingyingdizhi",SqlDbType.Char,10);
prmjingyingdizhi.Direction=ParameterDirection.Input;
cmd.Parameters.Add(prmjingyingdizhi);
SqlParameter prmzhucezhijin=new SqlParameter("@zhucezhijin",SqlDbType.Float);
prmzhucezhijin.Direction=ParameterDirection.Input;
cmd.Parameters.Add(prmzhucezhijin);
prmjingyingfanwei.Value=((TextBox)(e.Item.Cells[2].Controls[0])).Text;
prmjingyingdizhi.Value=((TextBox)(e.Item.Cells[3].Controls[0])).Text;
prmzhucezhijin.Value=((TextBox)(e.Item.Cells[4].Controls[0])).Text;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
cmd.Connection.Dispose();
cmd.Dispose();
DataGrid1.EditItemIndex= -1;
LoadData();
}
LoadData()是我定义的用于将修改的数据重新邦定, jingyingfanwei,jingyingdizhi,zhucezhijin是要更新的数据库表的3个项,主键是dengjimingcheng,表名是getixinxi,朋友们帮忙看看吧,谢谢啦!!!!