triest23
triest23

Reputation: 127

SQL Server stored procedure waiting parameter

I have a SQL Server stored procedure:

exec GetBob @id=3

But when I call this stored procedure in a function, I get the error:

The procedure or function expects the @id parameter

But @id is the correct parameter (get value).

public List<Bob> GetBob(int id)
{
    try
    {
        connection();
        con.Open();

        DynamicParameters param = new DynamicParameters();
        param.Add("@id", id);

        IList<Bob> EmpList = SqlMapper.Query<Bob>(con, "GetBob", param).ToList();
        con.Close();

        return EmpList.ToList();
    }
    catch (Exception)
    {
        throw;
    }
}

Upvotes: 1

Views: 189

Answers (1)

user9405863
user9405863

Reputation: 1514

Try this. You should be good. You need to provide command Type that it is Stored Procedure.

public List<Bob> GetBob(int id)
{
    try
    {
        connection();
        con.Open();
        DynamicParameters param = new DynamicParameters();
        param.Add("@id", id);
        IList<Bob> EmpList = SqlMapper.Query<Bob>(
                          con, "GetBob",param,commandType: CommandType.StoredProcedure).ToList();
        con.Close();
        return EmpList.ToList();
    }
    catch (Exception)
    {
        throw;
    }
}

Upvotes: 5

Related Questions