Link to home
Create AccountLog in
Avatar of rcowen00
rcowen00Flag for United States of America

asked on

Gridview Updating Error

I have a gridview I am attempting to update but I get the following error.  Any suggestions?  Thanks!

Error
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.

Source Error: 


Line 917:        Dim row = gdvRepairs.Rows(e.RowIndex)
Line 918:
Line 919:        dtable.Rows(row.DataItemIndex)("txtItem") = (CType((row.Cells(1).Controls(0)), TextBox)).Text
Line 920:        dtable.Rows(row.DataItemIndex)("txtFee") = (CType((row.Cells(2).Controls(0)), TextBox)).Text
Line 921:

Source File: C:\Users\Support\Documents\Visual Studio 2010\Projects\ProVal\ProVal\VendorPages\WebForms\CompEvalValuation.aspx.vb    Line: 919 

Stack Trace: 


[InvalidCastException: Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.]
   ProVal.CompEvalValuation.gdvRepairs_RowUpdating(Object sender, GridViewUpdateEventArgs e) in C:\Users\Support\Documents\Visual Studio 2010\Projects\ProVal\ProVal\VendorPages\WebForms\CompEvalValuation.aspx.vb:919
   System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +122
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +792
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +738
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +88
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +156
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9643314
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

Open in new window


GridView
  <asp:GridView ID="gdvRepairs" runat="server" CssClass="mGrid" Width="1172px" 
                            AutoGenerateColumns="False" AllowSorting="True" 
                            EmptyDataText="No Repairs Noted">
                            <Columns>
                                <asp:TemplateField HeaderText="ID">
                                    <EditItemTemplate>
                                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("OrdersRepairKey") %>'></asp:Label>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("OrdersRepairKey") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Repair Item" SortExpression="OrdersRepairItem">
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("OrdersRepairItem") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator15" runat="server" 
                                            ControlToValidate="txtItem" CssClass="ValidationError" Display="Dynamic" 
                                            ErrorMessage="Repair item required.">*</asp:RequiredFieldValidator>
                                        <asp:TextBox ID="txtItem" runat="server" Height="23px" 
                                            Text='<%# Bind("OrdersRepairItem") %>' TextMode="MultiLine" Width="300px"></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Fee" SortExpression="OrdersRepairCost">
                                    <ItemTemplate>
                                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("OrdersRepairCost") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                                            ControlToValidate="txtFee" CssClass="ValidationError" Display="Dynamic" 
                                            ErrorMessage="Repair cost requires whole number" 
                                            ValidationExpression="^(0|[1-9][0-9]*)$">*</asp:RegularExpressionValidator>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator14" runat="server" 
                                            ControlToValidate="txtFee" CssClass="ValidationError" Display="Dynamic" 
                                            ErrorMessage="Repair cost required.">*</asp:RequiredFieldValidator>
                                        <asp:TextBox ID="txtFee" runat="server" 
                                            Text='<%# Bind("OrdersRepairCost") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="btnEdit" runat="server" CausesValidation="False" 
                                            CommandName="Edit" >Edit</asp:LinkButton>
                                        &nbsp;&nbsp;
                                        <asp:LinkButton ID="btnDelete" runat="server" CausesValidation="False" 
                                            CommandName="Delete">Delete</asp:LinkButton>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
                                        &nbsp;&nbsp;
                                        <asp:LinkButton ID="btnCancel" runat="server" CausesValidation="False" 
                                            CommandName="Cancel">Cancel</asp:LinkButton>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <HeaderStyle ForeColor="White" />
                        </asp:GridView>

Open in new window



Update GridView
 Protected Sub gdvRepairs_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gdvRepairs.RowUpdating
        'Retrieve the table from the session object.
        Dim dtable = CType(Session("Repairs"), DataTable)

        'Update the values.
        Dim row = gdvRepairs.Rows(e.RowIndex)

        dtable.Rows(row.DataItemIndex)("txtItem") = (CType((row.Cells(1).Controls(0)), TextBox)).Text
        dtable.Rows(row.DataItemIndex)("txtFee") = (CType((row.Cells(2).Controls(0)), TextBox)).Text


        'Reset the edit index.
        gdvRepairs.EditIndex = -1

        'Bind data to the GridView control.

        BindData()
    End Sub

Open in new window


Load GridView
 Private Sub LoadRepairList()
        gdvRepairs.DataSource = Nothing
        gdvRepairs.DataBind()
        Dim cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("myprovalConnectionString").ConnectionString)
        Try
            Dim dt As New System.Data.DataTable
            Dim cmd As New System.Data.SqlClient.SqlCommand
            cmd.Connection = cnn
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.CommandText = "dbo.GetRepairsByOPK"
            cmd.Parameters.Add("@OrdersProductKey", Data.SqlDbType.Int).Value = Session("OrdersProductKey")
            Dim adpt As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(cmd)
            cnn.Open()
            adpt.Fill(dt)
            'Persist the table in the Session object.
            Session("Repairs") = dt
            BindData()
        Finally
            cnn.Close()
        End Try
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account