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

Posted on 2008-11-06
Medium Priority
Last Modified: 2013-12-17
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);
        DataGrid1.EditItemIndex = -1;

Open in new window

Question by:Rajar Ahmed
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22893593

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"
            Style="left: 173px; position: relative; top: 76px">
                <asp:TemplateColumn HeaderText="City">
                        <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>
                       <asp:Label ID="Label1" runat="server" Style="position: relative" Text='<%# Bind("city") %>'></asp:Label>
                <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:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn>

Open in new window

LVL 13

Accepted Solution

sm394 earned 2000 total points
ID: 22893608
try that
DropDownList cityname = (DropDownList)e.Item.Findcontrol("DropDownList1");

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question