简体   繁体   中英

C# wpf Get ID from a Listbox

Ive got a problem and need your help with it. Here is my Main Window:

主视窗

I fill the comboBox with the table tbl_Training From my Sql Server 2012 Database. On SelectionChange I get The ID and fill The ListBox:

 string selectedValue = Convert.ToString(comboTraining.SelectedValue);
 string sqlStrGridFill = @"SELECT (P.FirstName + ' ' + P.LastName) as Name 
                                    FROM tbl_Participant P
                                    INNER JOIN tbl_Training T
                                    ON P.ID_Training = T.ID_Training
                                    WHERE P.ID_Training ="+ selectedValue +";";


            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(sqlStrGridFill, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);

            lbParticipantList.ItemsSource = dt.DefaultView;
            lbParticipantList.DisplayMemberPath = Convert.ToString(dt.Columns["Name"]);
            lbParticipantList.SelectedValuePath = "ID_Participant";
            conn.Close();

now i want to get the ID_Participant on SelectChange from the ListBox. I tried a lot of things. This was my first and last try:

string selectedValue = Convert.ToString(lbParticipantList.SelectedValue);

selectedValue = ""
...Sadly it has to work until tommorow.

You don't have any column named "ID_Participant" in the result from the SQL.

The first line of your SQL should be

SELECT ID_Participant, (P.FirstName + ' ' + P.LastName) as Name 

This is why you're getting an empty string every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM