2

I am new in SQL and C# and I encountered this SQL error.

The parameterized query '(@pid nvarchar(4000),@desc nvarchar(4000),@cat nvarchar(4000),@p' expects the parameter '@pid', which was not supplied.

I really need help. Thanks!

 public void InsertRecord()
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO PRODUCTS VALUES (@pid, @desc, @cat, @price, @scode)", myCon);
        cmd.Parameters.AddWithValue("@pid", productID);
        cmd.Parameters.AddWithValue("@desc", description);
        cmd.Parameters.AddWithValue("@cat", category);
        cmd.Parameters.AddWithValue("@price", price);
        cmd.Parameters.AddWithValue("@scode", supplierCode);//corrected the "key codes"

        myCon.Open();
        cmd.ExecuteNonQuery();
        myCon.Close();//added these lines of codes
    }
Liam
  • 27,717
  • 28
  • 128
  • 190
YellowSubmarine
  • 151
  • 1
  • 4
  • 13

1 Answers1

5

You must check every parameters if they are null. If they are, you have to pass DBNull.Value.

SqlParameter pidParam = command.Parameters.AddWithValue("@pidParam", productID);
if (productID == null)
{
    pidParam.Value = DBNull.Value;
}

source: The parameterized query ..... expects the parameter '@units', which was not supplied

Community
  • 1
  • 1
Bartho Bernsmann
  • 2,393
  • 1
  • 25
  • 34