高手急救TextBox nwcCit=(TextBox)e.Item.Cells[0].Controls[0];问题
“/UserWeb”应用程序中的服务器错误。
--------------------------------------------------------------------------------指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 88: Response.Write("</script>");
行 89: */
行 90: TextBox nwcCit=(TextBox)e.Item.Cells[0].Controls[0];
行 91:
行 92: /*
源文件: e:\www\userweb\admin\egame.aspx.cs 行: 90 堆栈跟踪:
[InvalidCastException: 指定的转换无效。]
UserWeb.admin.egame.editDataGrid_UpdateCommand(Object source, DataGridCommandEventArgs e) in e:\www\userweb\admin\egame.aspx.cs:90
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +100
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +120
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292
当我用DATAGRID的更新事件时就出现这个问题,以前用的好好的呀
“/UserWeb”应用程序中的服务器错误。
--------------------------------------------------------------------------------指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 88: Response.Write("</script>");
行 89: */
行 90: TextBox nwcCit=(TextBox)e.Item.Cells[0].Controls[0];
行 91:
行 92: /*
源文件: e:\www\userweb\admin\egame.aspx.cs 行: 90 堆栈跟踪:
[InvalidCastException: 指定的转换无效。]
UserWeb.admin.egame.editDataGrid_UpdateCommand(Object source, DataGridCommandEventArgs e) in e:\www\userweb\admin\egame.aspx.cs:90
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +100
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +120
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292
当我用DATAGRID的更新事件时就出现这个问题,以前用的好好的呀
解决方案 »
- 数据库中的数据更新不了 求帮助
- IE6和IE8访问服务器时服务器取到的Request.RawUrl有什么区别
- 急,!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- asp.net 图片 数据库 读取(简单问题,马上给分)
- 有Master的页面,post hidden字段到其他页面,求个例子
- 弹出一个 showmodaldialog窗口, 是不是一定要关掉这个窗口,然后才能执行页面中的其他动作啊
- 超级难问题:iframe 连接到的页面 刷新调用的页面
- 在c#里面调用javascript
- 如何显示DropDownList的内容,从数据库里拿!!!
- 与flash交互!
- 初学asp.net,一个单aspx文件的问题!
- 下载功能求助!论坛数据库求助!会的人进来看看!答者有分!
{
//设置DataGrid控件的编辑项的索引为选择的当前项
editDataGrid.EditItemIndex = e.Item.ItemIndex;
//数据绑定
DataGridDataBind();
} private void editDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取得编辑行的关键字段的值
int empID = (int)editDataGrid.DataKeys[e.Item.ItemIndex];
TextBox nwcCit=(TextBox)e.Item.Cells[0].Controls[0];
//定义SQL语句
string sqlCom = "update gamename set gamename='" +nwcCit.Text.ToString()+"' where gamenameid="+empID;
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
//定义命令对象
SqlCommand cmd = new SqlCommand(sqlCom,conn);
////打开数据连接
conn.Open(); //执行SQL命令
cmd.ExecuteNonQuery();
//取消编辑
conn.Close();
editDataGrid.EditItemIndex = -1;
DataGridDataBind();
行 82:
行 83: //定义SQL语句
行 84: string sqlCom = "update gamename set gamename='" +nwcCit.Text+"' where gamenameid="+empID;
行 85: //定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
行 86: SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
if(e.Item.ItemType==ListItemType.EditItem)
來去掉dataGrid頭,因為在datagrid標頭上沒有找到控件
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
//创建数据适配器对象
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select a.*,g.gamename from acting a INNER JOIN gamename g ON a.gamenameid=g.gamenameid",conn);
//创建DataSet对象
DataSet ds = new DataSet();
//填充数据集
da.Fill(ds,"acting");
//进行数据绑定
DataTable mytable=ds.Tables["acting"];
DataColumn mycolumn= mytable.Columns.Add ("number",System.Type .GetType ("System.String"));
for (int i=0;i<mytable.Rows.Count ;i++)
{
mytable.Rows [i]["number"]=(i+1).ToString ();
}
editDataGrid.DataSource = mytable.DefaultView;
editDataGrid.DataBind();
conn.Close();
}
DATAGRID是这样帮定的
当点击修改的时候用 TextBox nwcCit=(TextBox)e.Item.Cells[0].Controls[1]; 取值就报错
异常详细信息: System.InvalidCastException: 指定的转换无效。
为什么用上面的查询之后用DataGrid修改的时候
用下面的这条语句不能取到值,而是提示下面的异常
TextBox nwcCit=(TextBox)e.Item.Cells[0].Controls[0];异常详细信息: System.InvalidCastException: 指定的转换无效。那位高手能指点一下呀
还有就是如果是在DataGrid中取你放进去的控件,控件的序号是1、3、5、7……这样的
所以试下
TextBox nwcCit=(TextBox)e.Item.Cells[0].Controls[1];
一定要注意控件的摆放顺序,按照1、3、5、7的序号来取~!
TextBox nwcCit=(TextBox)e.Item.Cells[0].Controls[0];