Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > [Newbie Problem]DataGrid OnUpdateCommand "InvalidCastException" problem

Reply
Thread Tools

[Newbie Problem]DataGrid OnUpdateCommand "InvalidCastException" problem

 
 
DC
Guest
Posts: n/a
 
      08-06-2007
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(.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(.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 " & _
"[EMail] = @EMail, [Staff] = @Staff " & _
"[Role] = @Role " & _
"WHERE [ID] = @ID"

Const strConnString as String = _
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Program Files\Common
Files\ODBC\Data Sources\user_info2.mdb"
Dim objConn as New OleDbConnection(strConnString)
objConn.Open()

Dim myCommand as OleDbCommand = new OleDbCommand(strSQL, objConn)
myCommand.CommandType = CommandType.Text

' Add Parameters to the SQL query

Dim parameterTitle as OleDbParameter = _
new OleDbParameter("@Title", OleDbType.VarWChar, 75)
parameterTitle.Value = strTitle
myCommand.Parameters.Add(parameterTitle)

Dim parameterForeName as OleDbParameter = _
new OleDbParameter("@ForeName", OleDbType.VarWChar, 75)
parameterForeName.Value = strForeName
myCommand.Parameters.Add(parameterForeName)

Dim parameterSurName as OleDbParameter = _
new OleDbParameter("@SurName", OleDbType.VarWChar, 75)
parameterSurName.Value = strSurName
myCommand.Parameters.Add(parameterSurName)

Dim parameterRoomNo as OleDbParameter = _
new OleDbParameter("@RoomNo", OleDbType.VarWChar, 75)
parameterRoomNo.Value = strRoomNo
myCommand.Parameters.Add(parameterRoomNo)

Dim parameterPhoneNo as OleDbParameter = _
new OleDbParameter("@PhoneNo", OleDbType.VarWChar, 75)
parameterPhoneNo.Value = strPhoneNo
myCommand.Parameters.Add(parameterPhoneNo)

Dim parameterEMail as OleDbParameter = _
new OleDbParameter("@EMail", OleDbType.VarWChar, 75)
parameterEMail.Value = strEMail
myCommand.Parameters.Add(parameterEMail)

Dim parameterStaff as OleDbParameter = _
new OleDbParameter("@Staff", OleDbType.Boolean, 75)
parameterStaff.Value = Convert.ToBoolean(strStaff)
myCommand.Parameters.Add(parameterStaff)

Dim parameterRole as OleDbParameter = _
new OleDbParameter("@Role", OleDbType.VarWChar, 75)
parameterRole.Value = strRole
myCommand.Parameters.Add(parameterRole)

Dim parameterID as OleDbParameter = _
new OleDbParameter("@ID", OleDbType.Integer)
parameterID.Value = ID
myCommand.Parameters.Add(parameterID)


myCommand.ExecuteNonQuery() 'Execute the UPDATE query

objConn.Close() 'Close the connection


'Finally, set the EditItemIndex to -1 and rebind the DataGrid
dgStaff.EditItemIndex = -1
BindData()
End Sub


Data Grid control definition

<aspataGrid id="dgStaff" runat="server"
AllowSorting="true"
OnSortCommand="dgStaff_Sort"
HeaderStyle-BackColor="Black"
HeaderStyle-ForeColor="White"
HeaderStyle-HorizontalAlign="Center"
HeaderStyle-Font-Bold="True"
CellPadding="4"
BackColor="lightyellow"
font-size="8"
AlternatingItemStyle-BackColor="yellow"
AutoGenerateColumns="false"
OnEditCommand="dgStaff_Edit"
OnUpdateCommand="dgStaff_Update"
OnCancelCommand="dgStaff_Cancel"
EditItemStyle-BackColor="lightgreen">

<Columns>
<asp:BoundColumn HeaderText="ID" DataField="ID" ReadOnly="True" />
<asp:BoundColumn HeaderText="Title" DataField="Title" />
<asp:BoundColumn HeaderText="Forename" DataField="ForeName" />
<asp:BoundColumn HeaderText="Surname" DataField="SurName" />
<asp:BoundColumn HeaderText="Room No" DataField="RoomNo" />
<asp:BoundColumn HeaderText="Phone" DataField="PhoneNo" />
<asp:BoundColumn HeaderText="E-Mail" DataField="EMail" />
<asp:BoundColumn HeaderText="Staff?" DataField="Staff" />
<asp:BoundColumn HeaderText="Role" DataField="Role" />
<asp:EditCommandColumn EditText="Edit" ButtonType="PushButton"
CancelText="Cancel" UpdateText="Update" />
</columns>

</aspataGrid>
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      08-06-2007
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" <(E-Mail Removed)> wrote in message
news:f97a0n$1ag$(E-Mail Removed)...
> 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(.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(.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 " & _
> "[EMail] = @EMail, [Staff] = @Staff " & _
> "[Role] = @Role " & _
> "WHERE [ID] = @ID"
>
> Const strConnString as String = _
> "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Program Files\Common
> Files\ODBC\Data Sources\user_info2.mdb"
> Dim objConn as New OleDbConnection(strConnString)
> objConn.Open()
>
> Dim myCommand as OleDbCommand = new OleDbCommand(strSQL, objConn)
> myCommand.CommandType = CommandType.Text
>
> ' Add Parameters to the SQL query
>
> Dim parameterTitle as OleDbParameter = _
> new OleDbParameter("@Title", OleDbType.VarWChar, 75)
> parameterTitle.Value = strTitle
> myCommand.Parameters.Add(parameterTitle)
>
> Dim parameterForeName as OleDbParameter = _
> new OleDbParameter("@ForeName", OleDbType.VarWChar, 75)
> parameterForeName.Value = strForeName
> myCommand.Parameters.Add(parameterForeName)
>
> Dim parameterSurName as OleDbParameter = _
> new OleDbParameter("@SurName", OleDbType.VarWChar, 75)
> parameterSurName.Value = strSurName
> myCommand.Parameters.Add(parameterSurName)
>
> Dim parameterRoomNo as OleDbParameter = _
> new OleDbParameter("@RoomNo", OleDbType.VarWChar, 75)
> parameterRoomNo.Value = strRoomNo
> myCommand.Parameters.Add(parameterRoomNo)
>
> Dim parameterPhoneNo as OleDbParameter = _
> new OleDbParameter("@PhoneNo", OleDbType.VarWChar, 75)
> parameterPhoneNo.Value = strPhoneNo
> myCommand.Parameters.Add(parameterPhoneNo)
>
> Dim parameterEMail as OleDbParameter = _
> new OleDbParameter("@EMail", OleDbType.VarWChar, 75)
> parameterEMail.Value = strEMail
> myCommand.Parameters.Add(parameterEMail)
>
> Dim parameterStaff as OleDbParameter = _
> new OleDbParameter("@Staff", OleDbType.Boolean, 75)
> parameterStaff.Value = Convert.ToBoolean(strStaff)
> myCommand.Parameters.Add(parameterStaff)
>
> Dim parameterRole as OleDbParameter = _
> new OleDbParameter("@Role", OleDbType.VarWChar, 75)
> parameterRole.Value = strRole
> myCommand.Parameters.Add(parameterRole)
>
> Dim parameterID as OleDbParameter = _
> new OleDbParameter("@ID", OleDbType.Integer)
> parameterID.Value = ID
> myCommand.Parameters.Add(parameterID)
>
>
> myCommand.ExecuteNonQuery() 'Execute the UPDATE query
>
> objConn.Close() 'Close the connection
>
>
> 'Finally, set the EditItemIndex to -1 and rebind the DataGrid
> dgStaff.EditItemIndex = -1
> BindData()
> End Sub
>
>
> Data Grid control definition
>
> <aspataGrid id="dgStaff" runat="server"
> AllowSorting="true"
> OnSortCommand="dgStaff_Sort"
> HeaderStyle-BackColor="Black"
> HeaderStyle-ForeColor="White"
> HeaderStyle-HorizontalAlign="Center"
> HeaderStyle-Font-Bold="True"
> CellPadding="4"
> BackColor="lightyellow"
> font-size="8"
> AlternatingItemStyle-BackColor="yellow"
> AutoGenerateColumns="false"
> OnEditCommand="dgStaff_Edit"
> OnUpdateCommand="dgStaff_Update"
> OnCancelCommand="dgStaff_Cancel"
> EditItemStyle-BackColor="lightgreen">
>
> <Columns>
> <asp:BoundColumn HeaderText="ID" DataField="ID" ReadOnly="True" />
> <asp:BoundColumn HeaderText="Title" DataField="Title" />
> <asp:BoundColumn HeaderText="Forename" DataField="ForeName" />
> <asp:BoundColumn HeaderText="Surname" DataField="SurName" />
> <asp:BoundColumn HeaderText="Room No" DataField="RoomNo" />
> <asp:BoundColumn HeaderText="Phone" DataField="PhoneNo" />
> <asp:BoundColumn HeaderText="E-Mail" DataField="EMail" />
> <asp:BoundColumn HeaderText="Staff?" DataField="Staff" />
> <asp:BoundColumn HeaderText="Role" DataField="Role" />
> <asp:EditCommandColumn EditText="Edit" ButtonType="PushButton"
> CancelText="Cancel" UpdateText="Update" />
> </columns>
>
> </aspataGrid>



 
Reply With Quote
 
 
 
 
DC
Guest
Posts: n/a
 
      08-07-2007
Teemu Keiski wrote:
> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
datalist vbCrLf & OnEditCommand/OnUpdateCommand Event Procedures Jim in Arizona ASP .Net Datagrid Control 0 12-29-2006 10:31 PM
datalist vbCrLf & OnEditCommand/OnUpdateCommand Event Procedures Jim in Arizona ASP .Net 0 12-28-2006 10:22 PM
DataGrid: OnUpdateCommand - getting the updated values Dan ASP .Net Datagrid Control 0 08-18-2005 10:16 AM
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM
DataGrid : dynamic BoundColumn & OnUpdateCommand =?Utf-8?B?S2FyaW5lIFByb290?= ASP .Net 1 01-27-2004 11:16 AM



Advertisments