• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

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

0
Rajar Ahmed
Asked:
Rajar Ahmed
  • 3
  • 2
  • 2
4 Solutions
 
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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now