using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Configuration; public partial class addexams : System.Web.UI.Page { #region string c = ConfigurationManager.ConnectionStrings["eexam"].ConnectionString; int i; #endregion protected void Page_Load(object sender, EventArgs e) { SqlConnection sc = new SqlConnection(c); sc.Open(); SqlCommand scom = new SqlCommand("select category from addexams", sc); SqlDataReader d = scom.ExecuteReader(); if (d.HasRows) { while (d.Read()) { RemoveDuplicateItems(DropDownList1); DropDownList1.Items.Add(d.GetString(0)); } } sc.Close(); } protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("add.aspx"); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection sc = new SqlConnection(c); sc.Open(); SqlCommand scom = new SqlCommand("select category from addexams", sc); SqlDataReader d = scom.ExecuteReader(); if (d.HasRows) { while (d.Read()) { RemoveDuplicateItems(DropDownList2); DropDownList2.Items.Add(d.GetString(0)); } } // sc.Close(); //} public static void RemoveDuplicateItems(DropDownList ddl) { for (int i = 0; i < ddl.Items.Count; i++) { ddl.SelectedIndex = i; string currValue = ddl.SelectedItem.ToString(); for (int counter = i + 1; counter < ddl.Items.Count; counter++) { ddl.SelectedIndex = counter; if (currValue == ddl.SelectedItem.ToString()) { ddl.Items.RemoveAt(counter); counter = counter - 1; } } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)