0

I am trying to make a C# program that will select a random data from my ms access database by clicking a button.. The error says that Data type mismatch in criteria expression. from the OleDbDataReader that i created..

This is what i currently achieved. Answers will be highly appreciated.

    {

        OleDbConnection connection = new OleDbConnection();

        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\miguel\Documents\QuotesGenFunny.accdb;
        Persist Security Info=False;";

        connection.Open();

        OleDbCommand command = new OleDbCommand();

        command.Connection = connection;
        command.CommandText = "SELECT TOP 1 * FROM Funny ORDER BY Rnd(-10000000*TimeValue(Now())*[Author])";

        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            listBox1.Items.Add(reader["Author"].ToString());
        }

        connection.Close();
    }
Kjartan
  • 18,591
  • 15
  • 71
  • 96

2 Answers2

0

Your seem to be trying to order your records by a randome value, which I don't think makes any sense.

Maybe it would be easier to skip a random number of records (just make sure it is less than the total number of records), then take the next record after that.

If your records have an ID that is is a number that increases by 1, you could probably just select a random record by generating a random number between 0 and the total nr of posts using C#.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
0

How to get random record from MS Access database

Check comments. It seems the correct syntax in your case is

ORDER BY Rnd(-(10000000*Funny)*Time())
Community
  • 1
  • 1
Fil
  • 1,032
  • 13
  • 29