Guys plz have a look:

I tried to update a particular column by using a dropdownlist while editing inside a gridview... I am getting the following Error on updating...

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

http://s19.postimage.org/ftytfxkyb/Error2.jpg

I am using the following code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustId" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" BackColor="Cyan" BorderColor="Red" ForeColor="Fuchsia" OnRowCancelingEdit="GridView1_RowCancelingEdit"  >
        <Columns>
          <asp:BoundField DataField="CustId" HeaderText="CustId" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="CustAddress" HeaderText="Address" />   
                            
                            
           <asp:TemplateField HeaderText="City" SortExpression="City">
           
           <EditItemTemplate>
           <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true"
           DataTextField="City" SelectedValue='<%# Bind("City") %>' AppendDataBoundItems="True">
           
         <asp:ListItem></asp:ListItem>
        <asp:ListItem>Kolkata</asp:ListItem>
        <asp:ListItem>Delhi</asp:ListItem>
        <asp:ListItem>Mumbai</asp:ListItem>
        <asp:ListItem>Chennai</asp:ListItem>
        <asp:ListItem>Bangalore</asp:ListItem>
        <asp:ListItem>Katihar</asp:ListItem>
        
           </asp:DropDownList>
           </EditItemTemplate>
           
           <ItemTemplate>
           <asp:Label ID="LabelCity" runat="server" Text='<%# Bind("City") %>'></asp:Label>
           </ItemTemplate>

           
           
           </asp:TemplateField>
            
              
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
    </asp:GridView>

and for rowupdating i am using the following code:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            GridViewRow row = GridView1.Rows[e.RowIndex];
            objCust.CustId = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
            objCust.CustAddress = ((TextBox)row.Cells[2].Controls[0]).Text;
            objCust.City =((TextBox)row.Cells[3].Controls[0]).Text;
            objCust.UpdateNewUser();
            GridView1.EditIndex = -1;
            bindGvEdit();
        }
        catch (Exception ex)
        {
            lblUpdate.Text = ex.Message;
        }
    }

Plz give some suggestion abt the problem..

Regards

Your <asp:BoundField DataField="CustAddress" HeaderText="Address" /> Address is a Bound Field and you are parsing it in Textbox......

Also, the control you are getting the City value from is a DropDownList, not a TextBox

Address is a boundfield and i am parsing in a textbox to display it in the gridview....
@ sufyan nd cherry can u plz tell me how to rectify the code...

Can you plz tell me how to parse dropdownlist value into the textbox

Guys finally solved the problem...

The code will be like this....

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try

        {
            GridViewRow row = GridView1.Rows[e.RowIndex];
            objCust.CustId = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
            objCust.CustAddress = ((TextBox)row.Cells[2].Controls[0]).Text;
            objCust.City= ((DropDownList)row.FindControl("ddlCity")).SelectedItem.Value;
            objCust.UpdateNewUser();
            GridView1.EditIndex = -1;
            bindGvEdit();
        }

        catch (Exception ex)
        {
            lblUpdate.Text = ex.Message;
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.