The SqlParameter is already contained by another SqlParameterCollection using SqlQuery Class in .Net

88 views Asked by At

I am invoking a stored procedure as follows:

var filterParameter = new SqlParameter("Column", filter);
var criteriaParameter = new SqlParameter("Criteria", criteria);
var countParameter = new SqlParameter("ItemPerPage", count);
var tenantParameter = new SqlParameter("TenantId", tenantId);
var pageParameter = new SqlParameter("Page", page);
string sortTypeQuery = "DESC";
if (sortType == OrderByType.Ascending) sortTypeQuery = "ASC";
var sortTypeParameter = new SqlParameter("SortType", sortTypeQuery);
var tableParameter = new SqlParameter("Table", "TenantSubscribers");

return await Context.Database.SqlQuery<string>(
    "EXEC [dbo].[Filters] @Column, @Criteria, @ItemPerPage, @TenantId, @SortType, @Table, @Page",
    filterParamter, criteriaParameter, countParameter, tenantParameter, sortTypeParameter, pageParameter,
    tableParameter, pageParameter).ToListAsync();

As a result it generates an exception:

The SqlParameter is already contained by another SqlParameterCollection

2

There are 2 answers

1
Muhammad Aftab On

You are providing pageParameter twice.

1
Alex Netkachov On

You have pageParameter two times. Should be only once.