简体   繁体   中英

Procedure or function “” expects parameter '', which was not supplied

I have a problem with Database thing. The stored Proc has to be called with either 3 or 4 parameters. If ImageID is not given, then it has to get into If loop and execute. if ImageID is given, execute the else part of the stored proc. But i have no idea why is that showing Procedure or function "" expects parameter '@ImageID', which was not supplied."

Thanks in 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);       
}

The Stored Proc is like this.

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

create your proc like this in that case

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

This will make ImageID an optional parameter

没有默认值,它需要一些价值

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

You have to pass the @ImageID parameter through code or else you can have a default value in your stored procedure for the @ImageID.

like @ImageID BIGINT = null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM