protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { DataTable categoryTable = new DataTable(); DataTable subCategoryTable = new DataTable(); SqlDataAdapter categoryDataAdapter = null; SqlDataAdapter subCategoryDataAdapter = null; using (SqlConnection connection = Utility.ServerConnection.Connection()) { connection.Open(); using (SqlCommand getAllCategories = Utility.ServerConnection.GetAllCategories(connection)) { categoryDataAdapter = new SqlDataAdapter(getAllCategories); } using (SqlCommand getAllSubCategories = Utility.ServerConnection.GetSubCategories(connection, 1)) { subCategoryDataAdapter = new SqlDataAdapter(getAllSubCategories); } categoryDataAdapter.Fill(categoryTable); subCategoryDataAdapter.Fill(subCategoryTable); Categories.DataSource = categoryTable; Categories.DataTextField = "CategoryName"; Categories.DataValueField = "CategoryID"; Categories.DataBind(); SubCategories.DataSource = subCategoryTable; SubCategories.DataTextField = "SubCategoryName"; SubCategories.DataValueField = "SubCategoryID"; SubCategories.DataBind(); Categories.SelectedIndex = 0; SubCategories.SelectedIndex = 0; } } catch { } } }
protected void Categories_SelectedIndexChanged(object sender, EventArgs e) { //Since values do not begin at 0, increment by 1. int selectedCategoryID = Categories.SelectedIndex + 1; DataTable subCategoryTable = new DataTable(); SqlDataAdapter dataAdapter = null; using (SqlConnection connection = Utility.ServerConnection.Connection()) { connection.Open(); using (SqlCommand getSubCategories = Utility.ServerConnection.GetSubCategories(connection, selectedCategoryID)) { dataAdapter = new SqlDataAdapter(getSubCategories); } dataAdapter.Fill(subCategoryTable); } SubCategories.DataSource = subCategoryTable; SubCategories.DataTextField = "SubCategoryName"; SubCategories.DataValueField = "SubCategoryID"; SubCategories.DataBind(); }