Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   [Newbie Problem]DataGrid OnUpdateCommand "InvalidCastException" problem (http://www.velocityreviews.com/forums/t527297-newbie-problem-datagrid-onupdatecommand-invalidcastexception-problem.html)

DC 08-06-2007 02:08 PM

[Newbie Problem]DataGrid OnUpdateCommand "InvalidCastException" problem
 
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 " & _
"[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

<asp:DataGrid 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>

</asp:DataGrid>

Teemu Keiski 08-06-2007 03:48 PM

Re: [Newbie Problem]DataGrid OnUpdateCommand "InvalidCastException" problem
 
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" <DC@dontbother.com> wrote in message
news:f97a0n$1ag$1@south.jnrs.ja.net...
> 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 " & _
> "[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
>
> <asp:DataGrid 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>
>
> </asp:DataGrid>




DC 08-07-2007 01:39 PM

Re: [Newbie Problem]DataGrid OnUpdateCommand "InvalidCastException"problem
 
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?


All times are GMT. The time now is 09:18 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.