D
DC
I'm getting the following casting error in my OnUpdateCommand
subroutine. Why would it be trying to cast a button control to a text
box in line 71? Any help would be greatly appreciated.
Exception Details: System.InvalidCastException: Unable to cast object of
type 'System.Web.UI.WebControls.Button' to type
'System.Web.UI.WebControls.TextBox'.
Source Error:
Line 69: Dim strEmail as String = CType(e.Item.Cells(7).Controls(0),
TextBox).Text
Line 70: Dim strStaff as String = CType(e.Item.Cells(8).Controls(0),
TextBox).Text
Line 71: Dim strRole as String = CType(e.Item.Cells(9).Controls(0),
TextBox).Text
OnUpdateCommand Code
Sub dgStaff_Update(sender As Object, e As DataGridCommandEventArgs)
'Read in the values of the updated row
Dim ID as Object = e.Item.Cells(1)
Dim strTitle as String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim strForeName as String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text
Dim strSurName as String = CType(e.Item.Cells(4).Controls(0),
TextBox).Text
Dim strRoomNo as String = CType(e.Item.Cells(5).Controls(0),
TextBox).Text
Dim strPhoneNo as String = CType(e.Item.Cells(6).Controls(0),
TextBox).Text
Dim strEmail as String = CType(e.Item.Cells(7).Controls(0), TextBox).Text
Dim strStaff as String = CType(e.Item.Cells(8).Controls(0),
TextBox).Text
Dim strRole as String = CType(e.Item.Cells(9).Controls(0), TextBox).Text
' ******* The line above is the one causing the error. ******
'Construct the SQL statement using Parameters
Dim strSQL as String = _
"UPDATE [user_table] SET [Title] = @Title, " & _
"[ForeName] = @ForeName, [SurName] = @SurName " & _
"[RoomNo] = @RoomNo, [PhoneNo] = @PhoneNo " & _
"(e-mail address removed)
subroutine. Why would it be trying to cast a button control to a text
box in line 71? Any help would be greatly appreciated.
Exception Details: System.InvalidCastException: Unable to cast object of
type 'System.Web.UI.WebControls.Button' to type
'System.Web.UI.WebControls.TextBox'.
Source Error:
Line 69: Dim strEmail as String = CType(e.Item.Cells(7).Controls(0),
TextBox).Text
Line 70: Dim strStaff as String = CType(e.Item.Cells(8).Controls(0),
TextBox).Text
Line 71: Dim strRole as String = CType(e.Item.Cells(9).Controls(0),
TextBox).Text
OnUpdateCommand Code
Sub dgStaff_Update(sender As Object, e As DataGridCommandEventArgs)
'Read in the values of the updated row
Dim ID as Object = e.Item.Cells(1)
Dim strTitle as String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim strForeName as String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text
Dim strSurName as String = CType(e.Item.Cells(4).Controls(0),
TextBox).Text
Dim strRoomNo as String = CType(e.Item.Cells(5).Controls(0),
TextBox).Text
Dim strPhoneNo as String = CType(e.Item.Cells(6).Controls(0),
TextBox).Text
Dim strEmail as String = CType(e.Item.Cells(7).Controls(0), TextBox).Text
Dim strStaff as String = CType(e.Item.Cells(8).Controls(0),
TextBox).Text
Dim strRole as String = CType(e.Item.Cells(9).Controls(0), TextBox).Text
' ******* The line above is the one causing the error. ******
'Construct the SQL statement using Parameters
Dim strSQL as String = _
"UPDATE [user_table] SET [Title] = @Title, " & _
"[ForeName] = @ForeName, [SurName] = @SurName " & _
"[RoomNo] = @RoomNo, [PhoneNo] = @PhoneNo " & _
"(e-mail address removed)