Search code examples
c#mysqlsqldatabaseoledb

System.Data.OleDb.OleDbException Require one or more parameters


I' using Visual Studio Enterprise 2015 WPF to do my Project and my database is a ms access file I'm not sure why I'm having this error Can someone

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: No value given for one or more required parameters.

Here is my Code

OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select `Name1`,`ID` from `Employee` WHERE `Name1` = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;

I have also tried

OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select Name1,ID from [Employee] WHERE Name1 = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;


OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select * from [Employee] WHERE Name1 = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;

But still the same Error

My Connection String

<connectionStrings>
<add name="Connection" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\wpfdb.accdb;Persist Security Info=False;"/>


Solution

  • In your command text lines, for example:

    > cmd.CommandText = "select `Name1`,`ID` from `Employee` WHERE `Name1` =
    > Jacob ";
    

    Jacob needs to be wrapped in quotes, not column and table names:

    cmd.CommandText = "select * Employee WHERE Name1 = 'Jacob' ";