Search code examples
c#oledbdataadapter

OleDbDataAdapter .Fill() method gives an exception saying No value given for one or more required parameters


       `OleDbConnection connect = new OleDbConnection(connectionString);
        connect.Open();
        string lastName = tBoxSurname.Text;
        OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT * FROM Players WHERE LastName=" + lastName, connect);

        DataSet dataset = new DataSet();
        adapter.Fill(dataset, "Players");

        dataGridView1.DataSource = dataset;
        dataGridView1.DataMember = "Players";
        connect.Close();`

I do not understand why it keeps giving that problem. I looked for different solutions and tried a lot of them but I keep getting the same problem. If someone could please explain what is happening because I have recently started learning about databases.


Solution

  • Try this one:

    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand command = new OleDbCommand("SELECT * FROM Players WHERE LastName = ?", connect);
    
    command.Parameters.Add("LastName", OleDbType.VarChar).Value = tBoxSurname.Text;
    
    adapter.SelectCommand = command;
    

    Please post your code next time as a snippet and not as a picture! :)