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

While updating am getting this error,

am using dropdownlist in edititemindex,
while saving am getting this error.
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.DropDownList'.
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
 
 
        DropDownList cityname = (DropDownList)e.Item.Cells[0].Controls[0];
        TextBox recipename = (TextBox)e.Item.Cells[1].Controls[0];
        TextBox restaurantname = (TextBox)e.Item.Cells[2].Controls[0];
        TextBox food = (TextBox)e.Item.Cells[3].Controls[0];
        TextBox foodtype = (TextBox)e.Item.Cells[4].Controls[0];
        TextBox fooddesc = (TextBox)e.Item.Cells[5].Controls[0];
        TextBox foodprice = (TextBox)e.Item.Cells[6].Controls[0];
        TextBox foodquantity = (TextBox)e.Item.Cells[7].Controls[0];
        Cmd.CommandText = "Update foodtable set city=@city,recipe=@recipe,resname=@resname,fname=@fname,ftype=@ftype,fdesc=@fdesc,price=@price,quantity=@quantity where no='" + DataGrid1.DataKeys[e.Item.ItemIndex] + "'";
        Cmd.Parameters.Add("@city", cityname.SelectedItem.Value);
        Cmd.Parameters.Add("@recipe", recipename.Text);
        Cmd.Parameters.Add("@fname", restaurantname.Text);
        Cmd.Parameters.Add("@ftype", foodtype.Text);
        Cmd.Parameters.Add("@fdesc", fooddesc.Text);
        Cmd.Parameters.Add("@price", foodprice.Text);
        Cmd.Parameters.Add("@quantity", foodquantity.Text);
        con.Open();
        Cmd.ExecuteNonQuery();
        con.Close();
        DataGrid1.EditItemIndex = -1;
        BindToGrid();
 
    }

Open in new window

LVL 18
Rajar AhmedConsultantAsked:
Who is Participating?
 
sm394Commented:
try that
DropDownList cityname = (DropDownList)e.Item.Findcontrol("DropDownList1");
0
 
Rajar AhmedConsultantAuthor Commented:

Inthis Line am getting error,
 DropDownList cityname = (DropDownList)e.Item.Cells[0].Controls[0];
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" DataKeyField="fid"
            OnCancelCommand="DataGrid1_CancelCommand" OnDeleteCommand="DataGrid1_DeleteCommand" OnUpdateCommand="DataGrid1_UpdateCommand"
            OnEditCommand="DataGrid1_EditCommand" 
            Style="left: 173px; position: relative; top: 76px">
            <Columns>
                <asp:TemplateColumn HeaderText="City">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" Style="position: relative" DataSourceID="SqlDataSource1" DataTextField="city" DataValueField="cid">
                        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:tonivtreepConnectionString2 %>"
                            SelectCommand="SELECT * FROM [citytable]"></asp:SqlDataSource>
                    </EditItemTemplate>
                    <ItemTemplate>
                       <asp:Label ID="Label1" runat="server" Style="position: relative" Text='<%# Bind("city") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:BoundColumn DataField="hid" HeaderText="recipe"></asp:BoundColumn>
                <asp:BoundColumn DataField="rid" HeaderText="restaurant"></asp:BoundColumn>
                <asp:BoundColumn DataField="fname" HeaderText="Food"></asp:BoundColumn>
                <asp:BoundColumn DataField="ftype" HeaderText="FoodType"></asp:BoundColumn>
                <asp:BoundColumn DataField="fdesc" HeaderText="Description"></asp:BoundColumn>
                <asp:BoundColumn DataField="price" HeaderText="Price"></asp:BoundColumn>
                <asp:BoundColumn DataField="quantity" HeaderText="Quantity"></asp:BoundColumn>
                <asp:EditCommandColumn CancelText="Cancel" EditText="edit" HeaderText="Update" UpdateText="Save">
                </asp:EditCommandColumn>
                <asp:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn>
            </Columns>
        </asp:DataGrid>&nbsp;&nbsp;</div>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.