我需要显示当前选择的名称

时间:2013-07-30 13:35:42

标签: c# wpf

WPF新手,我有一个包含数据库名称的组合框。当我选择名称时,先前选择的名称的详细信息会填充我的文本框。我需要显示当前选择的名称。有人能告诉我为什么会这样。

    private void comboBoxDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {
        string constring = "Data Source=tcp:*****.net;Initial Catalog=****;Persist Security Info=True;User ID=*******;Password=*****";

        string Query = "select * from Rewards where Name='" + comboBoxDisplay.Text + "' ;";
        SqlConnection conDataBase = new SqlConnection(constring);
        SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
        SqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {

                string sReid = myReader.GetInt32(0).ToString();
                string sName = myReader.GetString(1);


                txtRewardsId.Text = sReid;
                txtName.Text = sName;

1 个答案:

答案 0 :(得分:0)

您应该使用SelectedItem属性而不是Text

string Query = "select * from Rewards where Name='"
             + comboBoxDisplay.SelectedItem.ToString() + "' ;";