Answer the question
In order to leave comments, you need to log in
How to bind ListBox to database list and show values in text box?
On the form ListBox and TextBox.
When the form is launched, the ListBox is filled with data from the database.
MySqlConnection con = new MySqlConnection(AppSetting.ConnectonString());
MySqlCommand cmd;
cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM patients";
try
{
con.Open();
MySqlDataReader sdr = cmd.ExecuteReader();
// Очищаем список
MyListBox.Items.Clear();
// Загружаем данные
while (sdr.Read())
{
string sLastName = sdr.GetString("LastName");
MyListBox.Items.Add(sLastName);
}
// Закрываем соединение
con.Close();
}
catch (Exception)
{
MessageBox.Show("Ошибка");
}
MySqlConnection con = new MySqlConnection(AppSetting.ConnectonString());
MySqlCommand cmd;
cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM patients WHERE LastName='"+ MyListBox.Text +"';";
try
{
con.Open();
MySqlDataReader sdr = cmd.ExecuteReader();
// Загружаем данные
while (sdr.Read())
{
string sLastName = sdr.GetString("LastName");
txt_LastName.Text = sLastName;
}
// Закрываем соединение
con.Close();
}
catch (Exception)
{
MessageBox.Show("Ошибка");
}
cmd.CommandText = "SELECT * FROM patients WHERE LastName='"+ MyListBox.Text +"';";
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question