0

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

3
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by geniusvishal
0

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

Edited by sufyan2011: n/a

0

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...

0

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;
        }
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.