Code: Select all
// Connection and SQL strings
string SQL = "SELECT lab_name FROM lab";
// create a connection object
string ConnectionString = "Data Source=xyz;Initial Catalog=xyz;User ID=xyz;Password=xyz";
SqlConnection conn = new SqlConnection(ConnectionString);
// create command object
SqlCommand cmd = new SqlCommand(SQL, conn);
// open connection
conn.Open();
// call command's ExcuteReader
SqlDataReader reader = cmd.ExecuteReader();
try
{
while (reader.Read())
{
comboBox1.Items.Add(reader["lab_name"].ToString());
}
}
finally
{
// close reader and connection
reader.Close();
conn.Close();
}
So the question is how to get the selecteditem from combobox. I tried a couple of things, for example
comboBox1.SelectedItem.ToString();
but it doesn't seem to work..... I am sure am missing something. Thank you.