[Newbie Problem]DataGrid OnUpdateCommand "InvalidCastException" problem

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)
 
T

Teemu Keiski

Indexing starts at 0 not at 1. So issue is basically on every line in your
code accessing cells

For example

Dim ID as Object = e.Item.Cells(1)

should be

Dim ID as Object = e.Item.Cells(0)


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

DC said:
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)
 
D

DC

Teemu said:
Indexing starts at 0 not at 1. So issue is basically on every line in your
code accessing cells

For example

Dim ID as Object = e.Item.Cells(1)

should be

Dim ID as Object = e.Item.Cells(0)

Thanks a lot what a silly variable referencing error,

Im now getting

Compiler Error Message: BC30311: Value of type
'System.Web.UI.WebControls.TableCell' cannot be converted to 'Integer'.
on line 63.

Source Error:

Line 61:
Line 62: 'Read in the values of the updated row
Line 63: Dim ID as Integer = e.Item.Cells(0)
Line 64: Dim strTitle as String = CType(e.Item.Cells(1).Controls(0),
TextBox).Text
Line 65: Dim strForeName as String =
CType(e.Item.Cells(2).Controls(0), TextBox).Text

But If I use CType or CInt to convert the TableCell, I get the error...

BC30311: Value of type 'System.Web.UI.WebControls.TableCell' cannot be
converted to 'Integer'. for the line

Dim ID as Integer = CInt(e.Item.Cells(0))

I think I may have a basic conceptual problem with what is happening
here. How does one get a cell from a DataTable in edit mode into an
integer variable?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top