0

Trying to display a value from Access Database to display onto ListBox on a form. The previous form sends this form a string which is 'prevval'- for reference to the code. Not entirely sure what the issue is? Please Help!! The QuestionID is technically a number but is it an issue if im making it a string because its being presented on the ListBox?

Error System.Data.OleDb.OleDbException (0x80040E10): No value given for one or more required parameters

Code:

       try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "SELECT QuestionID FROM tblQuestions WHERE (Topic='" + prevval + "')";
            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                listQuestions.Items.Add(reader.ToString());
            }
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex);
        }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
nivlem
  • 39
  • 1
  • 2
    What is the type of `Topic` column and what is the value of `prevval` exactly? And of course, you should always use [parameterized queries](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/). This kind of string concatenations are open for [SQL Injection](http://en.wikipedia.org/wiki/SQL_injection) attacks. Also `reader.ToString()` returns it's full class name instead of a data inside of it. You need to use `reader[0]`or as a better `GetXXX` methods of it. – Soner Gönül Mar 24 '15 at 15:28
  • Where is the exception being thrown? – Jon Skeet Mar 24 '15 at 15:31
  • prevval can vary from different words, depending on the word, will depend on what QuestionID is obtained and what is next displayed. – nivlem Mar 24 '15 at 15:38
  • changing listQuestions.Items.Add(reader.ToString()); to listQuestions.Items.Add(reader[0]); Solved my issue, thank you! – nivlem Mar 24 '15 at 15:38

1 Answers1

1

Try this

listQuestions.Items.Add(reader["QuestionID"].ToString());
Kami
  • 365
  • 1
  • 10