Monday, 12 August 2013

Store some data from database

Store some data from database

public void fillComboBox()
{
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
}
I have this to add each employee 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?

No comments:

Post a Comment