[Newbie Problem]DataGrid OnUpdateCommand "InvalidCastException" problem

Discussion in 'ASP .Net' started by DC, Aug 6, 2007.

  1. DC

    DC Guest

    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, [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, Aug 6, 2007
    #1
    1. Advertising

  2. DC

    Teemu Keiski Guest

    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" <> wrote in message
    news:f97a0n$1ag$...
    > 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, [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> [/color]
     
    Teemu Keiski, Aug 6, 2007
    #2
    1. Advertising

  3. DC

    DC Guest

    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?
     
    DC, Aug 7, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ajit
    Replies:
    1
    Views:
    3,681
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  2. =?Utf-8?B?S2FyaW5lIFByb290?=

    DataGrid : dynamic BoundColumn & OnUpdateCommand

    =?Utf-8?B?S2FyaW5lIFByb290?=, Jan 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,422
    =?Utf-8?B?S2FyaW5lIFByb290?=
    Jan 27, 2004
  3. Jim in Arizona
    Replies:
    0
    Views:
    390
    Jim in Arizona
    Dec 28, 2006
  4. Dan

    DataGrid: OnUpdateCommand - getting the updated values

    Dan, Aug 18, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    172
  5. Jim in Arizona

    datalist vbCrLf & OnEditCommand/OnUpdateCommand Event Procedures

    Jim in Arizona, Dec 29, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    662
    Jim in Arizona
    Dec 29, 2006
Loading...

Share This Page