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);
}
Topiccolumn and what is the value ofprevvalexactly? And of course, you should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks. Alsoreader.ToString()returns it's full class name instead of a data inside of it. You need to usereader[0]or as a betterGetXXXmethods of it.