I have a dropdownlist DDL1
in asp.net webpage which is databound with "Product" column from all the tables in a particular database. Now my problem is, if i select one Product name from DDL1
, who are the customers having that product name should be bind another dropdownlist of DDL2
. I have table per customer. I am getting customer names based on the name of the column, but i want customer names based on the column value. What will be the query/stored procedure and code for that? (Asp.Net, Sql-2014, C#).
If I understood correctly, you have 7 tables with an schema similar to this:
Sno int,
Product varchar,
Picture varchar
First you populate a DropDownList with all possible product's name. Then you select one item of this DropDownList, for example: 'Pen', and you need to know which tables has a Product='Pen'.
One possible solution is building a function that filters every table and fill the second DropDownList.
string [] table_name = {"Table1","Table2",...};
private void FillSecondDDL(string product_name)
{
DDL2.Items.Clear();
foreach(string tname in table_name)
{
if (HasThisProduct(tname, product_name)
{
DDL2.Items.Add(tname);
}
}
}
private bool HasThisProduct(string table_name, string product_name)
{
string cmd = string.Format("select Sno from {0} where Product='{1}'"
, table_name
, product_name);
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable table;
SqlDataAdapter adapter = new SqlDataAdapter();
adp.SelectCommand = new SqlCommand(cmd, connection);
adp.Fill(table);
return table.Rows.Count > 0 ? true : false;
}
}
Just to give you an idea.
There are many different ways to achieve this. Here is one:
<body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Title" DataValueField="Id" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True"> </asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="Id" AutoPostBack="True"> </asp:DropDownList> </div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="[YOUR CONNECTION STRING]" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [Id], [Title] FROM [Products]"></asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="[YOUR CONNECTION STRING]" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Customers] WHERE ([ProductId] = @ProductId)"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="ProductId" PropertyName="SelectedValue" Type="Int32" DefaultValue="1" /> </SelectParameters> </asp:SqlDataSource> </form> </body>
The only C# code you need here is this:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.DropDownList2.DataBind();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.