Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i had a table named as 'addcategory'
in that there 4 cols 'category,subject,question,answer'
in aspx form i have two drop down lists one for add category and other to ad subject
first i want to add category in dropdown1 and from that i select one category then add the related subjects in dropdown2 i also need there is no repeated values in both drop down lists ,also if one category is the particular subjects which are under that category should be in the subject drop down list wt can i do ? plz hlp me in detailed thanx a lot in advance
my codings are below:

C#
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;
                }
            }
        }
    }

}


plz correct this and send the right codings
Posted
Updated 25-Mar-12 6:44am
v2
Comments
ProEnggSoft 25-Mar-12 12:45pm    
Edit: pre tag for C# code added - PES

Try this,

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection(c);
sc.Open();
SqlCommand scom = new SqlCommand("select distinct subjects from addexams where category = '" + DropDownList1.selectedItem.text + "'", sc);
SqlDataReader d = scom.ExecuteReader();
if (d.HasRows)
{
while (d.Read())
{
RemoveDuplicateItems(DropDownList2);
DropDownList2.Items.Add(d.GetString(0));

}
}

sc.Close();
}

I hope this will help you.
Yogesh Pednekar
 
Share this answer
 
Use 'DISTINCT' keyword.
select distinct category from addexams
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900