以下为DataGrid的UpdateCommand,我在编辑按钮里面有个更新的按钮,可是在TextBox更新后点'更新',却提示以下错误"异常详细信息: System.InvalidCastException: 指定的转换无效。
源错误: 行 147: //Update the database
行 148: string computerID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();//DataKeyField 为'computerID'
行 149: string computerName = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
行 150: string computerUser=((TextBox)e.Item.Cells[1].Controls[0]).Text;
行 151: SqlConnection con=DBOpen.createConnection();
"不知道是哪里的错误? private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//Update the database
string computerID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();//DataKeyField 为'computerID'
string computerName = ((TextBox)e.Item.Cells[0].Controls[0]).Text;//computerName为第一行第一列
string computerUser=((TextBox)e.Item.Cells[1].Controls[0]).Text;//computerUser为第二行第二列
SqlConnection con=DBOpen.createConnection();//建立了连接类
con.Open();
SqlCommand cmd=new SqlCommand("UPDATE computerMaintenance " +
"SET computerName = '" + computerName.Replace("'","''")+ "'," +
"computerUser = '" + computerUser.Replace("'","''")+ "'" +
" WHERE computerID = " + e.Item.Cells[0].Text); //数据库名称为computerMaintenance,我要实现更新computerName和computerUser
cmd.ExecuteNonQuery();
this.DataGrid1.EditItemIndex=-1;
this.BindToDataGrid();
}
源错误: 行 147: //Update the database
行 148: string computerID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();//DataKeyField 为'computerID'
行 149: string computerName = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
行 150: string computerUser=((TextBox)e.Item.Cells[1].Controls[0]).Text;
行 151: SqlConnection con=DBOpen.createConnection();
"不知道是哪里的错误? private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//Update the database
string computerID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();//DataKeyField 为'computerID'
string computerName = ((TextBox)e.Item.Cells[0].Controls[0]).Text;//computerName为第一行第一列
string computerUser=((TextBox)e.Item.Cells[1].Controls[0]).Text;//computerUser为第二行第二列
SqlConnection con=DBOpen.createConnection();//建立了连接类
con.Open();
SqlCommand cmd=new SqlCommand("UPDATE computerMaintenance " +
"SET computerName = '" + computerName.Replace("'","''")+ "'," +
"computerUser = '" + computerUser.Replace("'","''")+ "'" +
" WHERE computerID = " + e.Item.Cells[0].Text); //数据库名称为computerMaintenance,我要实现更新computerName和computerUser
cmd.ExecuteNonQuery();
this.DataGrid1.EditItemIndex=-1;
this.BindToDataGrid();
}
解决方案 »
- 服务端控制客户端
- JS 获取ADO.NET Data Services 返回值
- CSDN中的发帖与回帖积分是怎么处理的?
- windows2000下asp.net问题
- 为什么使用File.Exists语句会报错“索引超出了数组界限”??没道理啊
- 关于ViewState储存字符串数组的问题?
- 我要学asp了,介绍一些好的资料把!谢谢!
- VS2008生成项目诡异问题(bin目录下 model层是旧的,导致找不到类引用)
- 页面上有两个radiobutton1和radiobutton2,如何实现选中radiobutton1不执行验证控件验证,radiobutton2执行验证控件验证
- .net中数据绑定的难题,大家看看如何解决
- 到得文本编辑器中的值!
- 使用jmail有问题,请大家帮帮忙.
{
TextBox ClassNameText = (TextBox)e.Item.Cells[1].Controls[0];
string className = ClassNameText.Text;
int classID = Int32.Parse((e.Item.Cells[0].Text).ToString());
TextBox orderID2 = (TextBox)e.Item.Cells[5].Controls[0];
int orderID = Int32.Parse(orderID2.Text);
ActorClass.ModifyActorClass(className,classID,orderID); DataGrid1.EditItemIndex = -1;
if (Request.QueryString.Get("classID") != null)
Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where parentID=" + Request.QueryString.Get("classID") + " order by depth,orderID desc"));
else
Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where depth=1 order by depth,orderID desc"));
}
说明
(1) DataGrid 事件处理程序的格式
MethodName(Object sender, DataGridCommandEventArgs e)
(2) 更新按钮的说明
A. 获取编辑状态中的文本框
TextBox ClassNameText = (TextBox)e.Item.Cells[1].Controls[0];
string className = ClassNameText.Text;看看别人的代码