简体   繁体   中英

Store some data from database

I have this to add each employee's full name in the comboBox. Problem is where I will stored(hidden) the EmpID so that if an item is selected in the comboBox it will display the selected employee's EmpID in a textBox?

public void fillComboBox()
{
    comboBox1.Items.Add("Add Employee");
    using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
    {
        myDatabaseConnection.Open();

        using (SqlCommand mySqlCommand = new SqlCommand("Select EmployeeID, LastName, FirstName, MiddleName from Employee", myDatabaseConnection))
        {
            using (SqlDataReader sqlreader = mySqlCommand.ExecuteReader())
            {
                while (sqlreader.Read())
                {
                    string Lname = sqlreader.GetString(sqlreader.GetOrdinal("LastName"));
                    string Fname = sqlreader.GetString(sqlreader.GetOrdinal("FirstName"));
                    string Mname = sqlreader.GetString(sqlreader.GetOrdinal("MiddleName"));
                    string fullName = Lname + ", " + Fname + " " + Mname; 
                    comboBox1.Items.Add(fullName);
                }
            }
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //textBox1.Text = SelectedEmpID
        if (comboBox1.Text == "Add Employee")
        {
            EmployeeForm nf = new EmployeeForm();
            DialogResult res = nf.ShowDialog();
            if (res == DialogResult.OK)
            {
                comboBox1.Items.Clear();
                fillComboBox();
            }
        }
    }

do it like this

using (SqlConnection con = new SqlConnection(myConnectionString.ConnectionString))
    {
        string query = "Select EmployeeID, (LastName + ','+ FirstName+' '+ MiddleName) as Name from Employee";
        SqlDataAdapter dap = new SqlDataAdapter(query ,con);
        DataTable dt = new DataTable();
        dap.Fill(dt);
         comboBox1.DisplayMember = "Name";
         comboBox1.ValueMember = "EmployeeID";
        DataRow dr = dt.NewRow();
        dr[0] = -1;
        dr[1] = "Add Employee";
        dt.Rows.InsertAt(dr, 0);
        comboBox1.DataSource = dt;

    }

then

   private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
   {
       if(Convert.ToInt32(comboBox1.SelectedValue.ToString())==-1)
       {
        EmployeeForm nf = new EmployeeForm();
        DialogResult res = nf.ShowDialog();
        if (res == DialogResult.OK)
        {
            comboBox1.Items.Clear();
            fillComboBox();
        }
       }
    }

Note: If you want to react only when the user change the selected item in the combo box, then it is better to subscribe to SelectionChangeCommitted.

First of all i would create a class Employee . For example like this:

public class Employee
{
    public int EmployeeID {get;set;}
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string FullName { get { return LastName + ", " + FirstName + " " + MiddleName; } }

    public Employee(IDataRecord record)
    {
        this.EmployeeID = Convert.ToInt32(record["EmployeeID"]);
        this.LastName = record["LastName"].ToString();
        this.FirstName = record["FirstName"].ToString();
        this.MiddleName = record["MiddleName"].ToString();
    }
}

Using that class your fillBox() function will look like this:

 public void fillComboBox()
    {
        comboBox1.DisplayMember = "FullName";
        comboBox1.Items.Add("Add employee");
        using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();

            using (SqlCommand mySqlCommand = new SqlCommand("Select EmployeeID, LastName, FirstName, MiddleName from Employee", myDatabaseConnection))
            {
                using (SqlDataReader sqlreader = mySqlCommand.ExecuteReader())
                {
                    while (sqlreader.Read())
                    {
                        comboBox1.Items.Add(new Employee(sqlreader));
                    }
                }
            }
        }
    }

And of course SelectedIndexChangedEvent then:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
          var selecetedEmployee = comboBox1.SelectedItem as Employee;
          if (selecetedEmployee == null)
          {
               EmployeeForm nf = new EmployeeForm();
               DialogResult res = nf.ShowDialog();
               if (res == DialogResult.OK)
               {
                   comboBox1.Items.Clear();
                   fillComboBox();
               }
           }
           else
           {
                textBox1.Text = selecetedEmployee.EmployeeID.ToString();
           }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM