使用C#WPF我试图用数据绑定组合框填充文本框

时间:2013-05-16 22:59:15

标签: c# mysql wpf data-binding combobox

我有一些代码,但在完成另一个选择后填充文本框。如果我选择约翰,他的信息将不会填写,直到在约翰之后做出另一个选择。我想在选择后立即更新文本框。这是我的组合框代码:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string constring = "datasource=localhost;port=3306;username=root;password=jillian";
    string Query = "select * from test.edata where firstname = '" + ComboBox.Text +"' ;";

    MySqlConnection conDatabase = new MySqlConnection(constring);

    MySqlCommand cmdDatabase = new MySqlCommand(Query, conDatabase);

    MySqlDataReader myReader;

    try
    {
        conDatabase.Open();
        myReader = cmdDatabase.ExecuteReader();

        while (myReader.Read())
        {
            string sName = myReader.GetString("firstname");
            string sUName = myReader.GetString("username");
            string sLName = myReader.GetString("lastname");
            string sID = myReader.GetInt32("ID").ToString();  //retrieves the INT and then converts it to a String

            id_txt.Text = sID;
            firstname_txt.Text = sName;
            lastname_txt.Text = sLName;
            username_txt.Text = sUName;
        }
    }

    catch (Exception ex) { MessageBox.Show(ex.Message); }

    conDatabase.Close();
    //id_txt.Text = ComboBox.Text;
}

0 个答案:

没有答案