SQL Server Procedure has no parameters and arguments were supplied exception while my stored procedure accepts a parameter

balp4ylt  于 2023-10-15  发布在  其他
关注(0)|答案(2)|浏览(149)

Below is the stored procedure:

ALTER PROCEDURE Retrieve_Data 
      @noOfRecords INTEGER
AS
BEGIN TRY
    SET NOCOUNT ON;

    SELECT TOP(@noOfRecords) studentId
    FROM Student;
END TRY    
BEGIN CATCH    
   RETURN -101
END CATCH    
       
RETURN 0

Below is the C# code:

DataSet ds = null;

try
{
    using (SqlConnection conn = new SqlConnection(ConfigurationSettings.ConectionString))
    {
        using (SqlDataAdapter da = new SqlDataAdapter())
        {
            da.SelectCommand = new SqlCommand(storedProcedureName, conn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter param= new SqlParameter("@noOfRecords", SqlDbType.Int);
            param.Value = 10;
            da.SelectCommand.Parameters.Add(param);

            ds = new DataSet();
            da.Fill(ds, "result");

            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }

            return ds;
        }
    }
}

While executing the code I am getting a SQL exception:

Procedure Retrieve_Data has no parameters and arguments were supplied

xkftehaa

xkftehaa1#

The syntax of your stored procedure is incorrect. You need to provide the input parameters inside brackets

ALTER PROCEDURE [dbo].[Retrieve_Data]
(
    @noOfRecords INT
)
AS
BEGIN

Also you can try to clear your parameters inside the code:

if (conn.State == ConnectionState.Open)
{
   da.SelectCommand.Parameters.Clear();;
   conn.Close();
}
6ju8rftf

6ju8rftf2#

Change your stored procedure and wrap with BEGIN ... END

ALTER PROCEDURE [dbo].[Retrieve_Data]
      @noOfRecords INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
    SELECT TOP(@noOfRecords) studentId
    FROM Student;
END TRY    
BEGIN CATCH    
   RETURN -101
END CATCH    
RETURN 0; 
END

Second thing about ordering table Student using ORDER BY ...

相关问题