无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI

源码:SqlConnectionsqlcon=newSqlConnection(strCon);//定义查询字符串stringname=((TextBox)(GridVi... 源码: SqlConnection sqlcon = new SqlConnection(strCon);

//定义查询字符串
string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string borntime = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string money = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string address = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string code = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
string check = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim();

string updatestr = "update test set name='" + name + "', borntime='" + borntime + "' , money='" + money + "', address='" + address + "', code='" + code + "', check='" + check + "' where id=" + int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString().Trim());

后来改成如下就产生:未将对象引用设置到对象的实例提示错误
改后代码:
string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).FindControl("TextBox1")).Text.ToString().Trim();
string borntime = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).FindControl("TextBox1")).Text.ToString().Trim();
string money = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).FindControl("TextBox1")).Text.ToString().Trim();
string address = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).FindControl("TextBox1")).Text.ToString().Trim();
string code = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).FindControl("TextBox1")).Text.ToString().Trim();
string check = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).FindControl("TextBox1")).Text.ToString().Trim();
SqlCommand sqlcom = new SqlCommand(updatestr, sqlcon); //实例化SQL command对象
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
求高手帮忙解决一下。。。。。
展开
 我来答
匿名用户
2011-09-23
展开全部
同意楼上的说法,肯定在后台“编辑“的响应事件中,添加了类似FindControl的方法,但是在类型转换的时候,强制转换的类型和原有的类型不符!
具体的说,就是你讲System.Web.UI.LiteralControl转换成System.Web.UI.WebControls.DropDownList了!

追问
该怎么修改??????我数据库有几个字段,类型分别是:int的id;varchar的姓名;money的薪酬;varchar的地址;varchar类型的审核字段
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
小琳子Gk
2011-09-23 · 超过53用户采纳过TA的回答
知道小有建树答主
回答量:197
采纳率:0%
帮助的人:110万
展开全部
你可以试着强制转换一下看看!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式