datagrid dropdownlist..

this is the same code am using  without a datagrid , i mean this code works fine indivudally,

in other pagee i ve datagrid ,in edit template i ve three dropdown list , same result to b shown . as in code. but am not able to find id of dropdownlist . and also how can i do , selected change of dropdownlist in datagrid  .......??
protected void ddlcity_SelectedIndexChanged(object sender, EventArgs e)
    {
        string code = ddlcity.SelectedValue.ToString();
        string constr = ConfigurationManager.AppSettings["cons"];
        SqlConnection con1 = new SqlConnection(constr);
 
        SqlCommand recipename = new SqlCommand("select hid,recipe from recipetable where cid='" + code + "'", con1);
        SqlDataAdapter da_recipe = new SqlDataAdapter(recipename);
        DataSet ds = new DataSet();
        da_recipe.Fill(ds);
        ddlrecipe.DataSource = ds;
        ddlrecipe.DataTextField = "recipe";
        ddlrecipe.DataValueField = "hid";
        ddlrecipe.Items.Clear();
        ddlrecipe.DataBind();
 
        SqlCommand restname = new SqlCommand("select resttable.*,resname from resttable where hid='" + code + "'", con1);
        SqlDataAdapter da_restname = new SqlDataAdapter(restname);
        DataSet ds1 = new DataSet();
        da_restname.Fill(ds1);
        ddlres.DataSource = ds1;
        ddlres.DataTextField = "resname";
        ddlres.DataValueField = "rid";
        ddlres.Items.Clear();
        ddlres.DataBind();
 
    }
 
 
protected void ddlrecipe_SelectedIndexChanged(object sender, EventArgs e)
    {
        string code = ddlrecipe.SelectedValue.ToString();
        string constr = ConfigurationManager.AppSettings["cons"];
        SqlConnection con1 = new SqlConnection(constr);
        SqlCommand restname = new SqlCommand("select resttable.*,resname from resttable where hid='" + code + "'", con1);
        SqlDataAdapter da_restname = new SqlDataAdapter(restname);
        DataSet ds1 = new DataSet();
        da_restname.Fill(ds1);
        ddlres.DataSource = ds1;
        ddlres.DataTextField = "resname";
        ddlres.DataValueField = "rid";
        ddlres.Items.Clear();
        ddlres.DataBind();
    }
----------------------------------------

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.

Refael AckermannCommented:
Since you are adding the DDL to a dynamic data control, you will probably have more than one instance (e.g. one for each line of data). So the ddlCity & ddlres & ddlrecipe references are not relevant.
You should use the "sender" object referrence to find out who rised the event.

DropDownList ddl = (DropDownList)sender;

Open in new window

0

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
Obadiah ChristopherCommented:
I think if u post the code it might be more relevant.

But for obtaining the ddl in datagrid u'll have to write in the ItemDatabound event of the datagrid.


DropdownList ddl=e.Item.FindControl("ddlregion");
0
Rajar AhmedConsultantAuthor Commented:
Its a reall big code....
I had pasted the necessary things...
If its not enough i ll posttt the entire page....


I want the selected changed of dropdown list should happen inside the datagrid....
i dunno how to proceed.....
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" DataKeyField="fid"
            OnCancelCommand="DataGrid1_CancelCommand" OnDeleteCommand="DataGrid1_DeleteCommand" OnUpdateCommand="DataGrid1_UpdateCommand"
            OnEditCommand="DataGrid1_EditCommand" 
            Style="left: 68px; position: relative; top: 199px" AllowPaging="True" CellPadding="4" OnPageIndexChanged="DataGrid1_PageIndexChanged" ForeColor="#333333" GridLines="None" >
            <Columns>
                <asp:TemplateColumn HeaderText="City">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" Style="position: relative" DataSourceID="SqlDataSource1" DataTextField="city" DataValueField="cid" AppendDataBoundItems="true" SelectedItem='<%# Bind("city") %>' OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        <asp:ListItem Text="Change ur City" Value="0" />
                        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:tonivtreepConnectionString2 %>"
                            SelectCommand="SELECT * FROM [citytable]"></asp:SqlDataSource>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="DropDownList1"
                            ErrorMessage="Please Select any City" Style="position: relative" InitialValue="0">*</asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                       <asp:Label ID="Label1" runat="server" Style="position: relative" Text='<%# Bind("city") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="recipe">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Style="position: relative" Text='<%# Bind("recipe") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" Style="position: relative" DataSourceID="SqlDataSource2" DataTextField="recipe" DataValueField="hid" AppendDataBoundItems="true" SelectedItem='<%# Bind("resipe") %>' OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
                        <asp:ListItem Text="Change ur Recipe" Value="0" />
                        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:tonivtreepConnectionString3 %>"
                            SelectCommand="SELECT [hid], [recipe] FROM [recipetable]"></asp:SqlDataSource>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="DropDownList2"
                            ErrorMessage="Please Select Any Recipe" InitialValue="0" Style="left: 0px; position: relative;
                            top: -2px" Width="11px">* </asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="restaurant">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Style="position: relative" Text='<%# Bind("resname") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList3" runat="server" Style="position: relative" AppendDataBoundItems="true"  DataSourceID="SqlDataSource3"  DataTextField="resname" DataValueField="rid" SelectedItem='<%# Bind("resname1") %>'>
                        <asp:ListItem Text="Change Your restaurant" Value="0"></asp:ListItem>
                        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:tonivtreepConnectionString %>"
                            SelectCommand="SELECT [rid], [resname] FROM [resttable]"></asp:SqlDataSource>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="DropDownList3"
                            ErrorMessage="Please Select any Restaurant" InitialValue="0" Style="position: relative">*</asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="Food">
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Style="position: relative" Text='<%# Bind("fname") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("fname") %>'  Style="position: relative"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
                            ErrorMessage="*" Style="position: relative"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="FoodType">
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Style="position: relative" Text='<%# Bind("ftype") %>'></asp:Label>
                    </ItemTemplate>
                   
        </tr>
    </table>
 
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
 
        DropDownList ddlcity = (DropDownList)sender;
        DropDownList ddlrecipe = (DropDownList)sender;
        DropDownList ddlres = (DropDownList)sender;
        string code = ddlcity.SelectedValue.ToString();
        string constr = ConfigurationManager.AppSettings["cons"];
        SqlConnection con1 = new SqlConnection(constr);
 
        SqlCommand recipename = new SqlCommand("select hid,recipe from recipetable where cid='" + code + "'", con1);
        SqlDataAdapter da_recipe = new SqlDataAdapter(recipename);
        DataSet ds = new DataSet();
        da_recipe.Fill(ds);
        ddlrecipe.DataSource = ds;
        ddlrecipe.DataTextField = "recipe";
        ddlrecipe.DataValueField = "hid";
        ddlrecipe.Items.Clear();
        ddlrecipe.DataBind();
 
        SqlCommand restname = new SqlCommand("select resttable.*,resname from resttable where hid='" + code + "'", con1);
        SqlDataAdapter da_restname = new SqlDataAdapter(restname);
        DataSet ds1 = new DataSet();
        da_restname.Fill(ds1);
        ddlres.DataSource = ds1;
        ddlres.DataTextField = "resname";
        ddlres.DataValueField = "rid";
        ddlres.Items.Clear();
        ddlres.DataBind();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
 
 
        DropDownList ddlrecipe = (DropDownList)sender;
        DropDownList ddlres = (DropDownList)sender;
        string code = ddlrecipe.SelectedValue.ToString();
        string constr = ConfigurationManager.AppSettings["cons"];
        SqlConnection con1 = new SqlConnection(constr);
        SqlCommand restname = new SqlCommand("select resttable.*,resname from resttable where hid='" + code + "'", con1);
        SqlDataAdapter da_restname = new SqlDataAdapter(restname);
        DataSet ds1 = new DataSet();
        da_restname.Fill(ds1);
        ddlres.DataSource = ds1;
        ddlres.DataTextField = "resname";
        ddlres.DataValueField = "rid";
        ddlres.Items.Clear();
        ddlres.DataBind();
    }
}

Open in new window

0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Obadiah ChristopherCommented:
I think the above code should work fine.

The only thing that I can see is that the autopostback of the dropdownlist has not been set to true.

Try setting that for the dropdownlists
0
Rajar AhmedConsultantAuthor Commented:
Am getting this error...

Both DataSource and DataSourceID are defined on 'DropDownList1'.  Remove one definition.
0
Refael AckermannCommented:
Since you are changing the DataSource object explicitly, you should set the DataSourceID to null.
0
Rajar AhmedConsultantAuthor Commented:
I removed datasourceid  but still get that error message..
asp:TemplateColumn HeaderText="City">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" Style="position: relative" DataSourceID="SqlDataSource1" DataTextField="city" DataValueField="cid" AppendDataBoundItems="true" SelectedItem='<%# Bind("city") %>' OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        <asp:ListItem Text="Change ur City" Value="0" />
                        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:tonivtreepConnectionString2 %>"
                            SelectCommand="SELECT * FROM [citytable]"></asp:SqlDataSource>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="DropDownList1"
                            ErrorMessage="Please Select any City" Style="position: relative" InitialValue="0">*</asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                       <asp:Label ID="Label1" runat="server" Style="position: relative" Text='<%# Bind("city") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="recipe">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Style="position: relative" Text='<%# Bind("recipe") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" Style="position: relative"  AppendDataBoundItems="true" >
                        <asp:ListItem Text="Change ur Recipe" Value="0" />
                        </asp:DropDownList>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="DropDownList2"
                            ErrorMessage="Please Select Any Recipe" InitialValue="0" Style="left: 0px; position: relative;
                            top: -2px" Width="11px">* </asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="restaurant">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Style="position: relative" Text='<%# Bind("resname") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList3" runat="server" Style="position: relative" AppendDataBoundItems="true"  >
                        <asp:ListItem Text="Change Your restaurant" Value="0"></asp:ListItem>
                        </asp:DropDownList>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="DropDownList3"
                            ErrorMessage="Please Select any Restaurant" InitialValue="0" Style="position: relative">*</asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </asp:TemplateColumn>

Open in new window

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