0

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;
4

1 回答 1

0

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

string Query = "select * from Rewards where Name='"
             + comboBoxDisplay.SelectedItem.ToString() + "' ;";
于 2013-07-30T13:43:25.903 回答