无法将类型为“System.Web.UI.WebControls.DataControlLinkBut​​ton”的对象强制转换为“System.Web.UI.WebControls.TextBox”。 [英] Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.

查看:455
本文介绍了无法将类型为“System.Web.UI.WebControls.DataControlLinkBut​​ton”的对象强制转换为“System.Web.UI.WebControls.TextBox”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我得到一个例外,因为---

Hello,

I Am getting an exception as ---

Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.

Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
       Dim vartcktId As String = CType(row.Cells(0).Controls(0), TextBox).Text
       Dim varName As String = CType(row.Cells(1).Controls(0), TextBox).Text
       Dim varAge As String = CType(row.Cells(2).Controls(0), TextBox).Text
       Dim varSex As String = CType(row.Cells(3).Controls(0), TextBox).Text
       Dim varReservedClass As String = CType(row.Cells(4).Controls(0), TextBox).Text
       Dim varStatus As String = CType(row.Cells(5).Controls(0), TextBox).Text
       Dim varsourceId As String = CType(row.Cells(6).Controls(0), TextBox).Text
       Dim vardestinId As String = CType(row.Cells(7).Controls(0), TextBox).Text
       Dim vartrainNo As String = CType(row.Cells(8).Controls(0), TextBox).Text
       Dim vartcktBookDt As String = CType(row.Cells(9).Controls(0), TextBox).Text
       Dim varUserId As String = CType(row.Cells(10).Controls(0), TextBox).Text
       Dim varReserveDt As String = CType(row.Cells(11).Controls(0), TextBox).Text
       Dim sql As String = "Update Passenger set name='" & varName & "', age='" & varAge & "', sex='" & varSex & "', ReservedClass='" & varReservedClass & "', status='" & varStatus & "'where tcktId='" & vartcktId & "'"
       Dim sql1 As String = "Update Passenger2 set  sourceId='" & varsourceId & "', destinId='" & vardestinId & "' where tcktId='" & vartcktId & "'"
       Dim sql3 As String = "Update Reserves set  trainNo='" & vartrainNo & "', tcktBookdate='" & vartcktBookDt & "', userId='" & varUserId & "', ReserveDate='" & varReserveDt & "' where tcktId='" & vartcktId & "'"
       Dim st As String = ConfigurationManager.ConnectionStrings("TrainsConnectionString").ConnectionString
       Dim con As New SqlConnection(st)
       Dim cmd As New SqlCommand(sql, con)
       Dim cmd2 As New SqlCommand(sql1, con)
       Dim cmd3 As New SqlCommand(sql3, con)
       con.Open()
       cmd.ExecuteReader()
       cmd2.ExecuteReader()
       cmd3.ExecuteReader()
       con.Close()
       GridView1.EditIndex = -1



请帮助我!!


please help me !!

推荐答案

正如错误消息所示:您正在尝试将对象强制转换为非在它的继承树中。特别是在这种情况下,你的一个GridView列是一个DataControlLinkBut​​ton,你不能把它转换成TextBox,因为它不是一个。



这有点像摩托车和面包车之间的区别:它们都是汽车的例子,但是你不能把第一个变成第二个并装满你的家具!



所以看看你的GridView,看看你有什么对象 - 然后适当地投射它们。



BTW:不要连接字符串来构建一个SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
As the error message says: you are trying to cast an object to a type that is not in it''s inheritance tree. Specifically in this case, one of your GridView columns is a DataControlLinkButton, and you can''t cast that to a TextBox, because it isn''t one.

It''s a bit like the difference between a motorcycle and a van: they both are examples of Motor Vehicles but you can''t change the first into the second and fill it full of your furniture!

So look at your GridView, and see what objects you have - then cast them appropriately.

BTW: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


这篇关于无法将类型为“System.Web.UI.WebControls.DataControlLinkBut​​ton”的对象强制转换为“System.Web.UI.WebControls.TextBox”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆