Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the stored procedure
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_UpdateProducts]
(
@PID int,
@Quantity int
)
AS
BEGIN
SET NOCOUNT ON;
UPDATE tblProducts SET PQuantity = @Quantity WHERE PID = @PID
END





This is table tblProducts
create table tblProducts
(
PID int identity(1,1) primary key,
PName   nvarchar(MAX),
PPrice money,
PSelPrice money,
PBrandID int,
PCategoryID int,
PSubCatID int,
PGender int,
PQuantity int,
PDescription nvarchar(MAX),
PProductDetails nvarchar(MAX),
PMaterialCare  nvarchar(MAX),
FreeDelivery int,
[30DayRet]  int,
COD       int,
Constraint [FK_tblProducts_ToTable] FOREIGN KEY ([PBrandID]) REFERENCES [tblBrands] ([BrandID]),
Constraint [FK_tblProducts_ToTable1] FOREIGN KEY ([PCategoryID]) REFERENCES [tblCategory] ([CatID]),
Constraint [FK_tblProducts_ToTable2] FOREIGN KEY ([PSubCatID]) REFERENCES [tblSubCategory] ([SubCatID]),
Constraint [FK_tblProducts_ToTable3] FOREIGN KEY ([PGender]) REFERENCES [tblGender] ([GenderID])
)







<pre> protected void btnBuyNow_Click(object sender, EventArgs e)
    {

        Int32 UserID = Convert.ToInt32(Session["USERID"].ToString());

        //This will add +1 to current quantity using PID

        int PID = Convert.ToInt32(Request.QueryString["PID"]);

        using (SqlConnection con = new SqlConnection(CS))
        using (SqlCommand cmd = new SqlCommand("SP_UpdateProducts", con) { CommandType = CommandType.StoredProcedure })
        {
            DataTable dt = new DataTable();
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(dt);
            }

            foreach (DataRow row in dt.Rows)
            {

                int myQty = row.Field<int>("Qty");
                int PQuantity = row.Field<int>("PQuantity");
                if (myQty < PQuantity)
                {
                    int updateQty = row.Field<int>("Qty");
                    cmd.Parameters.AddWithValue("@Quantity", PQuantity - updateQty);
                    cmd.Parameters.AddWithValue("@PID", PID);
                    cmd.ExecuteNonQuery();

                    con.Open();
                    Response.Redirect("OrderConfirmation.aspx");
                }
            }
        
    }
    }


What I have tried:

I have check the @PID between stored procedure and the code, but it get error below:

tblProducts does not updated and get error Procedure or function 'SP_UpdateProducts' expects parameter '@PID', which was not supplied.
Posted
Updated 17-Mar-21 9:30am

1 solution

The SP requires two parameters @PID and @Quantity - which you appear to provide. But ... that code doesn't set the C# variable PID to anything, nor can we see the declaration - so there is a good chance that it's not an integer, and is in fact null. A null value passed as a parameter is the same as not providing the parameter itself, so there is a good chance that this causes your error.

Use the debugger, and look at PID before you add it to the parameters. That should tell you what is going on.
 
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