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);
        }
4
  • 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. This kind of string concatenations are open for 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. Commented Mar 24, 2015 at 15:28
  • Where is the exception being thrown? Commented Mar 24, 2015 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. Commented Mar 24, 2015 at 15:38
  • changing listQuestions.Items.Add(reader.ToString()); to listQuestions.Items.Add(reader[0]); Solved my issue, thank you! Commented Mar 24, 2015 at 15:38

1 Answer 1

1

Try this

listQuestions.Items.Add(reader["QuestionID"].ToString());
Sign up to request clarification or add additional context in comments.

2 Comments

That's true but this is not the reason of this exception.
Correct. I'm assuming that prevval is correctly passed as string and has a value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.