title: How to bind a drop-down control to a data source in ASP.NET
body: <p>I am new to C#.</p>
  
  <p>I have a project to create an HR system and I created a page to add employees but the supervisor asked me to create a drop down list that displays the department when adding a new employee.</p>
  
  <p>I don't know how to start and what I should do first. I already added a drop down list from the tools but I don't know how to choose the data source and the name and value of it. Should I choose the department table or the employee table?</p>
  
  <pre><code>public partial class _Default : Page
  {
  
      private String strcon = ConfigurationManager.ConnectionStrings["hr"].ConnectionString;
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
  
              bindgrideview();
      }
      protected void bindgrideview()
      {
          SqlConnection strcon1 = new SqlConnection(strcon);
          strcon1.Open();
          string ADDStr = "SELECT F_name , L_name , salary FROM Employee ";
          SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
          DataTable table = new DataTable();
  
  
          SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);
  
          adapter.Fill(table);
          GridView1.DataSource = table;
          GridView1.DataBind();
  
      }
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          string F_name = TextBox1.Text;
          string L_name = TextBox2.Text;
          int status = 1;
          string salarystr = TextBox3.Text.ToString();
          int salary = Int32.Parse(salarystr);
          SqlConnection strcon1 = new SqlConnection(strcon);
          strcon1.Open();
          string ADDStr = "ADDEMP";
          SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
          ADDCmd.CommandType = CommandType.StoredProcedure;
          ADDCmd.Parameters.AddWithValue("@F_name", F_name);
          ADDCmd.Parameters.AddWithValue("@L_name", L_name);
          ADDCmd.Parameters.AddWithValue("@status", status);
          ADDCmd.Parameters.AddWithValue("@salary", salary);
  
          ADDCmd.ExecuteNonQuery();
          bindgrideview();
          TextBox1.Text = "";
          TextBox2.Text = "";
      }
  </code></pre>
  
  <p>And this is a screen shot of my page:
  <a href="http://store2.up-00.com/Nov12/YXb11858.png" rel="noreferrer">http://store2.up-00.com/Nov12/YXb11858.png</a></p>
  
  <p>this is the final code there is no error but the dropdown list have no item :( </p>
  
  <pre><code>public partial class _Default : Page
  {
  
      private String strcon = ConfigurationManager.ConnectionStrings["hr"].ConnectionString;
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
  
              bindgrideview();
      }
      protected void bindgrideview()
      {
          SqlConnection strcon1 = new SqlConnection(strcon);
          strcon1.Open();
          string ADDStr = "SELECT F_name , L_name , salary FROM Employee ";
          SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
          DataTable table = new DataTable();
  
  
          SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);
  
          adapter.Fill(table);
          GridView1.DataSource = table;
          GridView1.DataBind();
  
      }
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          string F_name = TextBox1.Text;
          string L_name = TextBox2.Text;
          int status = 1;
          string salarystr = TextBox3.Text.ToString();
          int salary = Int32.Parse(salarystr);
          SqlConnection strcon1 = new SqlConnection(strcon);
          strcon1.Open();
          string ADDStr = "ADDEMP";
          SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
          ADDCmd.CommandType = CommandType.StoredProcedure;
          ADDCmd.Parameters.AddWithValue("@F_name", F_name);
          ADDCmd.Parameters.AddWithValue("@L_name", L_name);
          ADDCmd.Parameters.AddWithValue("@status", status);
          ADDCmd.Parameters.AddWithValue("@salary", salary);
  
          ADDCmd.ExecuteNonQuery();
          bindgrideview();
          TextBox1.Text = "";
          TextBox2.Text = "";
          TextBox3.Text = "";
      }
      protected void bindDepartments()
      {
          SqlConnection strcon1 = new SqlConnection(strcon);
          strcon1.Open();
          string ADDStr = "SELECT ID,department_name FROM Department ";
          SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
          DataTable table = new DataTable();
  
  
          SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);
  
          adapter.Fill(table);
  
          DropDownList1.DataSource = table;
          DropDownList1.DataValueField = "ID";
          DropDownList1.DataTextField = "department_name";
          DropDownList1.DataBind();
  
      }
  
  }
  </code></pre>