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

Posted on 2008-11-06
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

    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

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

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now