No value given for one or more required parameters dr=ExecuteReader()

2.1k views Asked by At
private void PopuniListu(){
    listBox1.Items.Clear();

    conn = new OleDbConnection(connString);
    conn.Open();

    cmd.Connection = conn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT Imeiprezimeautora FROM Tabela1";

    dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        listBox1.Items.Add(dr.GetValue(0));
    }

    dr.Close();
    conn.Close();
 }

Here is error:

No value given for one or more required parameters.

dr = cmd.ExecuteReader()

1

There are 1 answers

0
Steve On

When using an OleDb provider (with MS-Access in particular, but I can't exclude the same for other providers) the parsing engine looks if every part of the column list in the SELECT clause is present in the table queried. If, for any reason (usually a typo in the column name), the engine cannot find the corresponding column it treats the name as a parameter and expects that a parameter is supplied in the OleDbCommand.Parameters collection.
If no parameters is present then the above error is thrown.

Another possibility is the fact that you don't initialize the cmd variable in the code above.
This implies that you are using a global variable for the OleDbCommand.
A variable that may have been used for another query and its parameter collection is not empty.

Try to add this line before executing the query

cmd.Parameters.Clear();
cmd.CommandText = "SELECT Imeiprezimeautora FROM Tabela1";

However, if this is the problem, then I really suggest you to rethink this approach and avoid global variables for this kind of work.
There is no measurable performace penalty in defining and initializing an OleDbCommand locally