YellowSubmarine
YellowSubmarine

Reputation: 151

The parameterized query expects the parameter which was not supplied c# SQL

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
    }

Upvotes: 2

Views: 5566

Answers (1)

Bartho Bernsmann
Bartho Bernsmann

Reputation: 2503

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

Upvotes: 5

Related Questions