I have two databse table
1: Sector
2: Project
In Sector table there is 4 sectors(sector1,sector2,sector3,sector4)
and if sector1 is selected then then related project shown
Code
Sector ::
Project ::I have two databse table
1: Sector
2: Project
In Sector table there is 4 sectors(sector1,sector2,sector3,sector4)
and if sector1 is selected then then related project shown
Sector ::
Project ::Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Mar 9, 2023, 1:12 AM EST
@Garima use this
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ddlsector.DataSource = BindAllSector(); ddlsector.DataTextField = "SectorName"; ddlsector.DataValueField = "SectorID"; ddlsector.DataBind(); ddlsector.Items.Insert(0, new ListItem("Select Sector", "0")); } } protected void ddlsector_SelectedIndexChanged(object sender, EventArgs e) { int sectorId = Convert.ToInt32(ddlsector.SelectedValue); if (sectorId > 0) // If a valid sector is selected { ddlproject.DataSource = BindProjectBySector(sectorId); ddlproject.DataTextField = "ProjectName"; // Display field name ddlproject.DataValueField = "ProjectID"; // Value field name ddlproject.DataBind(); ddlproject.Items.Insert(0, new ListItem("Select Project", "0")); } else { ddlproject.Items.Clear(); ddlproject.Items.Insert(0, new ListItem("Select Project", "0")); } } public DataSet BindProjectBySector(int sectorId) { DataSet ds= new DataSet(); using(SqlConnection con= new SqlConnection("your Connection string")) { SqlDataAdapter da= new SqlDataAdapter("select * from project where sectorid="+sectorId,con); da.Fill(ds); } return ds; } public DataSet BindAllSector() { DataSet ds= new DataSet(); using(SqlConnection con= new SqlConnection("your Connection string")) { SqlDataAdapter da= new SqlDataAdapter("select * from Sector",con); da.Fill(ds); } return ds; }Jignesh KumarRank #91 · 22.9K reputationPosted Mar 9, 2023, 12:48 AM EST
Hello Grishma,
Please try below url which has step by step explanation for cascade dropdown,
https://www.c-sharpcorner.com/UploadFile/009464/cascading-one-dropdownlist-with-other-dropdownlist-in-asp-ne/
https://www.aspsnippets.com/Articles/Populate-Cascading-DropDownList-from-Database-in-ASPNet-Example.aspx
Vishal YelveRank #178 · 13.6K reputationPosted Mar 9, 2023, 12:51 AM EST
Please refer below link.
https://www.aspsnippets.com/Articles/Creating-Cascading-DropDownLists-in-ASP.Net.aspx
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 9, 2023, 12:53 AM EST
Hello Garima,
To cascade the dropdown from the database, you can follow these steps:
1. Retrieve the data from the database table "Sector" and bind it to the dropdown "ddlsector".
You can do this in the Page_Load event of your code-behind file as follows:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ddlsector.DataSource = GetData("SELECT * FROM Sector"); ddlsector.DataTextField = "SectorName"; // Display field name ddlsector.DataValueField = "SectorID"; // Value field name ddlsector.DataBind(); ddlsector.Items.Insert(0, new ListItem("Select Sector", "0")); // Add default item } }Here, "GetData" is a custom function that returns a DataTable containing the data retrieved from the database using the specified SQL query. You can implement this function as per your database connection and query.
2 Handle the SelectedIndexChanged event of "ddlsector" and retrieve the related projects from the database table "Project" and bind them to the dropdown "ddlproject".
You can do this as follows:
protected void ddlsector_SelectedIndexChanged(object sender, EventArgs e) { int sectorId = Convert.ToInt32(ddlsector.SelectedValue); // Get the selected sector ID if (sectorId > 0) // If a valid sector is selected { ddlproject.DataSource = GetData("SELECT * FROM Project WHERE SectorID = " + sectorId); ddlproject.DataTextField = "ProjectName"; // Display field name ddlproject.DataValueField = "ProjectID"; // Value field name ddlproject.DataBind(); ddlproject.Items.Insert(0, new ListItem("Select Project", "0")); // Add default item } else // If "Select Sector" is selected { ddlproject.Items.Clear(); // Clear the dropdown ddlproject.Items.Insert(0, new ListItem("Select Project", "0")); // Add default item } }Here, "SectorID" and "ProjectID" are assumed to be the primary keys of "Sector" and "Project" tables, respectively, and "SectorID" is a foreign key in the "Project" table referencing the "Sector" table. Also, "ProjectName" and "SectorName" are assumed to be the display field names in the "Project" and "Sector" tables, respectively.
Finally, don't forget to add the OnSelectedIndexChanged event handler for "ddlsector" in the markup as follows:
Here, AutoPostBack="True" ensures that the page is posted back to the server when the dropdown selection is changed, triggering the SelectedIndexChanged event.
Thanks
Naimish Makwana
Garima BansalRank #1,555 · 1K reputationPosted Mar 9, 2023, 1:03 AM EST
Naimish Makwana
I am getting an error in GetData method "The name "GetData" doesnt exists in current context
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 9, 2023, 1:07 AM EST
Hello Garima,
The "GetData" method referred to in the code snippet I provided is a custom method that you need to implement yourself. This method is responsible for retrieving data from the database and returning it as a DataTable.
Thanks
Naimish Makwana