2
votes

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
    }
1
is productID null by any chance? - Liam
You are using the INSERT syntax that requires all the fields values to be passed for insertion, do you have just 5 fields in your table PRODUCTS? - Steve
I can't enable the productID textbox. I don't know why. - YellowSubmarine
I mean the textbox is enabled but I can't type anything into it. - YellowSubmarine
Windows Forms Application in C# - YellowSubmarine

1 Answers

5
votes

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