0
votes

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'. this error display when i update the record i want to update the record by datagridview by depend on the id .

 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    SqlConnection conn = new SqlConnection(conctiioon);
    int userid = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
    GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
    //TextBox textName = (TextBox)row.Cells[0].Controls[0];
    TextBox textName = (TextBox)row.Cells[1].Controls[0];
    TextBox textprice = (TextBox)row.Cells[2].Controls[0];//here the error display 

    GridView2.EditIndex = -1;
    conn.Open();
    //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
    SqlCommand cmd = new SqlCommand("update items set name='" + textName.Text + "',price='" + textprice.Text + "' where id='" + userid + "'", conn);
    cmd.ExecuteNonQuery();
    conn.Close();
    gvbind();
}

        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
            OnRowDeleting="GridView2_RowDeleting" OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating"
            CellPadding="4" ForeColor="#333333" GridLines="None">

            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="S.No." />
                <asp:BoundField DataField="name" HeaderText="Name" />
                <asp:TemplateField HeaderText="Price">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Price") %>'
                            CssClass="stock">
                        </asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <a href="#" id='<%# Eval("id") %>' class="updatebutton">
                            <img border="0" src="Images/update.png" alt="Delete" />
                        </a>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField   ShowEditButton="true" />
                <asp:CommandField ShowDeleteButton="true" />
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
3
You are casting the cell value to textbox which is actual a literal control. In bellow line TextBox textName = (TextBox)row.Cells[1].Controls[0];Sain Pradeep
yes how can handel thismagdy
cast the name field control to literal and get the literal text like you did with textbox control.Sain Pradeep
the error display at this "textprice " how can i cast it to literalmagdy
Are you sure that error is coming while casting cell[2] control?Sain Pradeep

3 Answers

0
votes

Please Means the System.Web.UI.WebControl in NameSapce(UperCase)

0
votes

I mocked your gridview and implemented a simple datasource. You are assuming the price textbox is index 0 when there are 3 controls in that cell. The first control of that cell is not your textbox hence the cast fail.

Instead of assuming what the indexes are just find the control using the GridViewRow you already located.

TextBox textprice = (TextBox)(row.FindControl("TextBox1"));
0
votes

it will be

TextBox textprice = (TextBox)Gridview2.Rows[e.Rowindex].FindControl("TextBox1");