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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

sm394Commented:
try that
DropDownList cityname = (DropDownList)e.Item.Findcontrol("DropDownList1");

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.