The procedure or function "" expects a parameter '' that has not been set

I have a problem with the database. A stored Proc must be called with 3 or 4 parameters. If ImageID is not specified, then it should go into the If loop and execute. if ImageID is specified, follow the rest of the stored procedure. But I don’t know why this is shown by the Procedure or function "expects the parameter '@ImageID' which was not specified."

Thanks at Advance

if (!hasImage)
{
    parameters = new SqlParameter[]
    {
        new SqlParameter("@LocationID", LocationID),
        new SqlParameter("@PrimaryID", 
            Convert.ToInt32(CategoryList[i].ToString().Split(',')[0])),
        new SqlParameter("@SecondaryID", 
            Convert.ToInt32(CategoryList[i].ToString().Split(',')[1]))
    };

    SqlHelper.ExecuteNonQuery(
        DbConnString, 
        System.Data.CommandType.StoredProcedure,
        "TempUpdateMerchantCategories_Insert", 
        parameters);
}
else
{
    parameters = new SqlParameter[]
    {
        new SqlParameter("@LocationID", LocationID),
        new SqlParameter("@PrimaryID", 
            Convert.ToInt32(CategoryList[i].ToString().Split(',')[0])),
        new SqlParameter("@SecondaryID", 
            Convert.ToInt32(CategoryList[i].ToString().Split(',')[1])),
        new SqlParameter("@ImageID", 
            Convert.ToInt64(ImageData[j].ToString().Split(',')[0]))
    };
    SqlHelper.ExecuteNonQuery(
        DbConnString, 
        System.Data.CommandType.StoredProcedure,
        "TempUpdateMerchantCategories_Insert", 
        parameters);       
}

Stored Proc is as follows.

CREATE PROCEDURE [dbo].[TempUpdateMerchantCategories_Insert]
(
@LocationID BIGINT,
@PrimaryID INT,
@SecondaryID INT,
@ImageID BIGINT)

AS
BEGIN

if (@ImageID is null)

BEGIN
SET NOCOUNT ON;

INSERT INTO TempMerchant_Location_Category(

LocationID,
PrimaryID,
SecondaryID)

VALUES (
@LocationID,
@PrimaryID,
@SecondaryID)

END

ELSE

BEGIN
SET NOCOUNT ON;

INSERT INTO TempMerchant_Location_Category( 
LocationID,
PrimaryID,
SecondaryID,
ImageID)  

VALUES(
@LocationID,
@PrimaryID,
@SecondaryID,
@ImageID)

END
END
+3
source share
3 answers

create your proc like this in this case

    CREATE PROCEDURE [dbo].[TempUpdateMerchantCategories_Insert] 
( @LocationID BIGINT, 
@PrimaryID INT, 
@SecondaryID INT, 
@ImageID BIGINT = null)

This will make ImageID an optional parameter.

+9
source

CREATE PROCEDURE [dbo].[TempUpdateMerchantCategories_Insert] ( @LocationID BIGINT, @PrimaryID INT, @SecondaryID INT, @ImageID BIGINT = null)
0

You need to pass the @ImageID parameter using code, otherwise you may have a default value in your stored procedure for @ImageID.

like @ImageID BIGINT = null

0
source

Source: https://habr.com/ru/post/1763984/


All Articles