Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created one form containing 2 dropdownmenu(dpmtList amd empList)
accessing items through database.One dropmenu contains list of department (from database) and other dropmenu is list of employees of specified department which is selected

i have used nested if else statement and always i get list of employees which are in else statement...
coding is below

created two functions
loademploeelist()
loaddepartmentlist()

C#
protected void Page_Load(object sender, EventArgs e)
    {
        
            loademploeelist();
           loaddepartmentlist();
        
    }
 protected void loaddepartmentlist()
    {
        SqlConnection CON;
        SqlCommand COM;
        SqlDataReader READER;

        string ConnectionString = ConfigurationManager.ConnectionStrings["EMP"].ConnectionString;
        CON = new SqlConnection(ConnectionString);
        COM = new SqlCommand("Select department from dpmt", CON);

        try
        {
            CON.Open();
            READER = COM.ExecuteReader();
            dpmtList.DataSource = READER;
            dpmtList.DataValueField = "department";
            dpmtList.DataBind();
            READER.Close();
            
        }

        catch
        {
            CON.Close();
        }
      
    }
 protected void loademploeelist();
    {
                   SqlConnection CON;
                        SqlCommand COM;
            
            SqlDataReader READER;

            string ConnectionString = ConfigurationManager.ConnectionStrings["EMP"].ConnectionString;
            CON = new SqlConnection(ConnectionString);

            if (dpmtList.Text == "Administrartor")
            {
                COM = new SqlCommand("Select Name,ID from AD",CON);
            }

            else if (dpmtList.Text == "hr")
            {
                COM = new SqlCommand("Select Name,ID from Her", CON);
               
            }
            else if (dpmtList.Text == "IT")
            {
               COM = new SqlCommand("Select Name,ID from IT", CON);


            }
            else 
            {
            COM = new SqlCommand("Select Name,ID from HD",CON);

            }
           
            
            CON.Open();
            READER = COM.ExecuteReader();
            empList.DataSource = READER;
            empList.DataValueField = "ID";
            empList.DataTextField = "Name";
            empList.DataBind();

            READER.Close();

            CON.Close();

        }
    }

here administerartor,HR IT ,HD are department

Pls do tell me abut my mistake and rectify it
regards,
Posted

I would suggest not using separate tables for the same information. Create one table with a column that indicates what department the record belongs to. It will make the database more extensible and your code much easier. The dropdowns could be populated with a simple cascading pattern.
 
Share this answer
 
Comments
[no name] 29-Jun-11 9:21am    
Valid Point Mark. My 5 for you.
Call this loademploeelist() function in selected index Changed event of
your Department dropdown

take that selected value/text whichever is your Requirement
and on the basis of that value/text bind your employee dropdown

also Make Department dropdown autopostback property = true;/xml>
 
Share this answer
 
handle selectedidexchange events for dropdown and call your methods

and check pagepostback on pageload

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           loademploeelist();
           loaddepartmentlist();
       }
   }
 
Share this answer
 
Could you add following also where you are binding dpmtList and then check again

MIDL
dpmtList.DataTextField  = "department";
 
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