Why I get “System.Data.DataRowView” instead of real values in my Listbox?

前端 未结 12 824
一向
一向 2020-11-29 08:24

I\'m hoping someone can help. But whenever I run my code and try to view a highscore all I get back in my listbox is System.Data.DataRowView.

12条回答
  •  我在风中等你
    2020-11-29 09:12

    Like I said in the comments, please add lstNames.DataBind() to your code.

    MySqlConnection myConn = new MySqlConnection(connStr);
    
    string sqlStr = "SELECT CONCAT(Name, ' ', Score) as NameAndScore " + 
                    "FROM highscore ORDER BY Score DESC";
    
    MySqlDataAdapter dAdapter = new MySqlDataAdapter(sqlStr, myConn);
    DataTable dTable = new DataTable();
    dAdapter.Fill(dTable);
    dAdapter.Dispose();
    lstNames.DisplayMember = "NameAndScore";
    lstNames.ValueMember = "NameAndScore";
    lstNames.DataSource = dTable;
    

    EDIT:

    Can you try this instead:

    MySqlConnection myConn = new MySqlConnection(connStr);
    
        string sqlStr = "SELECT CONCAT(Name, ' ', Score) as NameAndScore " + 
                        "FROM highscore ORDER BY Score DESC";
    
      myConn .Open();
    
      SqlCommand cmd = new SqlCommand(sqlStr, SQLConnection1);
    
    
          SqlDataReader rd = cmd.ExecuteReader();
          while (rd.Read())
          {
            lstNames.Items.Add(rd[0]);
          }
          rd.Close();
          rd.Dispose();
          myConn.Close();
    

提交回复
热议问题