Click here to Skip to main content
15,888,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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'.
in the code :
VB
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 !!
Posted
Updated 23-Apr-13 8:23am
v2
Comments
Richard C Bishop 23-Apr-13 14:09pm    
Read the error, it tells you exactly what is wrong. Debug it and find out which line it is and fix it. If you are not sure what any of those words mean in the exception, look them up and learn them as they are basic terms in .Net.

1 solution

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900