Answer the question
In order to leave comments, you need to log in
How to populate TextBox from database using listbox?
There are several TextBox and ListBox components on the form. The data is loaded into the ListBox from a remote database built with PHPMyAdmin.
MySqlConnection con = new MySqlConnection(AppSetting.ConnectonString()); // Подключился к базе
con.Open();
MySqlCommand cmd;
cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM patients";
MySqlDataReader sdr = cmd.ExecuteReader();
DataTable dtRecords = new DataTable();
dtRecords.Load(sdr);
//Заполнил ListBox
MyListBox.DataSource = new BindingSource(dtRecords, null);
MyListBox.DisplayMember = "LastName";
MyListBox.ValueMember = "id";
Answer the question
In order to leave comments, you need to log in
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("Ошибка");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question