日期:2014-05-18  浏览次数:20511 次

运行后报错,可就是查不出原因。(一Gridview问题)问题解决立刻结帖。
我想在用Gridview绑定数据后进行更新和删除操作,在编码实现Updating事件时报错。相关信息如下,烦请各位大虾帮忙看看。谢谢!!!


//更新
        protected   void   GridView3_RowUpdating(object   sender,   GridViewUpdateEventArgs   e)
        {
                string   sqlupdate   =   "update   TABLE4     SET   t002=@t002     where   t001=@t001 ";
                SqlConnection   conn   =   new   SqlConnection(SqlconString.SqlconnectionString);
                SqlCommand   cmd   =   new   SqlCommand(sqlupdate,   conn);
                cmd.Parameters.Add( "@t001 ",   SqlDbType.VarChar,   4);
                cmd.Parameters.Add( "@t002 ",   SqlDbType.VarChar,   100);
                cmd.Parameters[ "@t001 "].Value   =GridView3.DataKeys[e.RowIndex][ "t001 "].ToString();
                cmd.Parameters[ "@t002 "].Value   =   ((TextBox)(GridView3.Rows[e.RowIndex].Cells[2].Controls   [0])).Text.ToString();
                conn.Open();
                cmd.ExecuteNonQuery();
                GridView3.EditIndex   =   -1;
                conn.Close();
                BindData();
             
        }


报错信息如下:
用户代码未处理InvalidCastException

unable   to   cast   object   of   type   'system.web.UI.webcontrols.datacontrollinkbutton '   to   type 'system.web.UI.webcontrols.textbox '.
排错提示:
当从一个数字执行强制转换时,值必须是一个小于无限大的数字。确保源类型可以转换为目标类型。


在线等,如果大家还有什么不清楚的,可以在线提问,菜鸟在这里谢谢各位大虾了。

------解决方案--------------------
cmd.Parameters[ "@t002 "].Value = ((TextBox)(GridView3.Rows[e.RowIndex].Cells[2].Controls [0])).Text.ToString();

这句错了。。你把它转换为 TextBox 何意?难道你的sql 参数是一个TextBox 控件 = =!
------解决方案--------------------
改成如下看行不?
cmd.Parameters[ "@t002 "].Value = ((TextBox)(GridView3.Rows[e.RowIndex].Cells[2].Controls [0])).Text;

------解决方案--------------------
GridView3.Rows[e.RowIndex].Cells[2].FindControl( "控件ID "),这样写才正确
------解决方案--------------------
'system.web.UI.webcontrols.datacontrollinkbutton ' to type 'system.web.UI.webcontrols.textbox '.

明顯類型錯誤,linkbutton ...textbox...看看是否Cells[2]找錯了
------解决方案--------------------
type 'system.web.UI.webcontrols.datacontrollinkbutton ' to type 'system.web.UI.webcontrols.textbox '

这个是说你找到的那个控件是datacontrollinkbutton类型,不能转为textbox的
楼主看一下是不是找错位置了
------解决方案--------------------
cmd.Parameters[ "@t002 "].Value = ((TextBox)(GridView3.Rows[e.RowIndex].Cells[2].Controls [0])).Text.ToString();