-1

I'm doing a chart using data into my database in a C# program. I have a list of users and i want to display in pseudo random order.

I've tried this code

void Button1Click(object sender, EventArgs e)
    {
        Connessione.Open();
        MySqlDataAdapter SDA=new MySqlDataAdapter("SELECT RAND() concorrente, id FROM Classifiche WHERE taglia='small' AND categoria='agility'",Connessione);
        DataTable DATA= new DataTable();
        SDA.Fill(DATA);
        dataGridView1.DataSource=DATA; 
        Connessione.Close();
    }

In this case, though, the query show me random numbers instead random strings. How can I fix it?

I've tried the solutions that was posted here How to request a random row in SQL? but I can't resolve the problem

Community
  • 1
  • 1

2 Answers2

0

You have to use RAND() if you use 'MySQL' database and NEWID() if you use 'Sql Server' in the ORDER BY clause of your query as following:

SELECT concorrente, id FROM Classifiche WHERE taglia='small' AND categoria='agility' ORDER BY RAND()
S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61
  • @Steve what can I use instead? –  Jan 26 '17 at 08:43
  • please debug your code, put a breakpoint after SDA.Fill(DATA) and check if DATA DataTable has any row. another thing: re-check if your WHERE clause has some matching result – S.Serpooshan Jan 26 '17 at 08:51
  • @S.Serp the code works. If I don't use RAND() I can display my data, and I tried other conditions just to try and everytime the program runs well –  Jan 26 '17 at 09:00
  • 2
    can you edit your question with exactly the updated code you test now after adding `RAND()` function? please also add `MySQL` tag to its keywords – S.Serpooshan Jan 26 '17 at 09:56
0

Use the newid() instead

SELECT concorrente, id FROM Classifiche WHERE taglia='small' AND categoria='agility' ORDER BY newid()
Wilmer
  • 138
  • 1
  • 8