Link to home
Start Free TrialLog in
Avatar of Rajar Ahmed
Rajar AhmedFlag for India

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Refael Ackermann
Refael Ackermann
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rajar Ahmed

ASKER

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Am getting this error...

Both DataSource and DataSourceID are defined on 'DropDownList1'.  Remove one definition.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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