Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am experiencing an error 'No value given for one or more parameters required ' when executing the following code:

C#
private void availbleproduct_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string connstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\OBS\OBSAccounts.accdb";
            string productname = availbleproduct.Rows[e.RowIndex].Cells["prodname"].Value.ToString();
            string sql = "SELECT prodname FROM inventory WHERE prodname=" + productname + "";
            OleDbConnection conn = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            OleDbDataReader reader;
            conn.Open();

            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                string proname = reader["prodname"].ToString();
                prod_name.Text = proname;
            }
        }
Posted
Updated 30-Aug-15 4:39am
v2

Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
The chances are that that will cure your problem at the same time.
C#
string sql = "SELECT prodname FROM inventory WHERE prodname=@PN";
using (OleDbConnection conn = new OleDbConnection(connstring))
   {
   using (OleDbCommand cmd = new OleDbCommand(sql, conn))
      {
      cmd.Parameters.AddWithValue("@PN",  productname);
      conn.Open();
      using (OleDbDataReader reader = cmd.ExecuteReader())
          {
          while (reader.Read())
              {
              ...

But do note that that will display only the last such value in the table - if there are more than one, only the last one returned will be displayed. Personally, I'd check the count and make sure there was only one.
 
Share this answer
 
First of all, do not concatenate values directly to the SQL statement. This leaves you vulnerable to SQL injections. Instead use OleDbParameter[^].

So the query should look something like

C#
string connstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\OBS\OBSAccounts.accdb";
string productname = availbleproduct.Rows[e.RowIndex].Cells["prodname"].Value.ToString();
string sql = "SELECT prodname FROM inventory WHERE prodname=@prodname";
using (OleDbConnection conn = new OleDbConnection(connstring)) {
   using (OleDbCommand cmd = new OleDbCommand(sql, conn)) {
      OleDbDataReader reader;
      conn.Open();
      adapter.SelectCommand.Parameters.Add("@prodname", OleDbType.VarChar, 100).Value = productname;

      reader = cmd.ExecuteReader();

      while (reader.Read()) {
         string proname = reader["prodname"].ToString();
         prod_name.Text = proname;
      }
   }
}

Also check that the prodname variable contains proper value for the query (for example isn't empty).
 
Share this answer
 
v2
It might be because you're missing the quotes

C#
string sql = "SELECT prodname FROM inventory WHERE prodname='" + productname + "'";


However this is a bad idea as it leaves you open to sql injection attacks, you should use parameterised queries instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900