Click here to Skip to main content
15,891,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the code is for select value from Usertype combo box
combo box contain values like (hr, production, executive) like that.

if someone select hr, next combo box will load all employees related to hr from sql database

Combo box names = UserType_CMBbox , UserName_CMBbox


C#
{
            String Usertype = UserType_CMBbox.SelectedValue.ToString();
            //Select User Type from user type Combo box

            UserName_CMBbox.Items.Clear();
            DataSet ds = new DataSet();
            ds.Clear();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=Unicella_Data;Integrated Security=True";
            con.Open();
            SqlDataAdapter adpi = new SqlDataAdapter("Select distinct User_Name from Login_Table Where User_Type='" + Usertype + "'", con);
            adpi.Fill(ds);
            UserName_CMBbox.DataSource = ds.Tables[0];
            UserName_CMBbox.DisplayMember = "User_Name";
            con.Close();
        }


the problem is data will not load into the other combo box
Posted
Comments
Kschuler 4-Nov-13 17:11pm    
What kind of project is this? Win forms? Web? (Please click the Improve question and add tags to your question to specify this) What is a UserType_CMBbox? And you really should look into using parameters instead of directly concatting values into your SQL statement.
BulletVictim 5-Nov-13 0:51am    
SqlDataAdapter adpi = new SqlDataAdapter("Select distinct User_Name from Login_Table Where User_Type='" + Usertype_CMBox.SelectedItem.ToString() + "'", con);

Should fix the problem you have

1 solution

Hi Promod,

Try this,
C#
  String Usertype = UserType_CMBbox.SelectedValue.ToString();
   //Select User Type from user type Combo box

   UserName_CMBbox.DataSource = null;
   DataSet ds = new DataSet();
   SqlConnection con = new SqlConnection();
   con.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=Unicella_Data;Integrated Security=True";
   con.Open();
   SqlDataAdapter adpi = new SqlDataAdapter("Select distinct User_Name from Login_Table Where User_Type='" + Usertype + "'", con);
   adpi.Fill(ds);
   UserName_CMBbox.DataSource = ds.Tables[0];
   UserName_CMBbox.DisplayMember = "User_Name";
   con.Close();



I hope this will help you.

Thank's
Mohan G
 
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