I'm using SqlParameter, when I use it twice I get this error:
{"Otro SqlParameterCollection ya contiene SqlParameter."}
Here I use it the first time: SP2
using (ApplicationDbContext db1 = new ApplicationDbContext())
{
SqlParameter Fechainicio = new SqlParameter()
{
ParameterName = "@Fechainicio",
DbType = DbType.DateTime, //Tipo de datos
Value = @ViewBag.PlaFechaInicio
};
SqlParameter FechaFinal = new SqlParameter()
{
ParameterName = "@FechaFinal",
DbType = DbType.DateTime, //Tipo de datos
Value = @ViewBag.PlaFechaFin
};
SqlParameter empleadoid = new SqlParameter()
{
ParameterName = "@empleadoid",
DbType = DbType.String, //Tipo de datos o Int32 si es entero y le quitas las comillas al valor en Value
Value = item.EmpId
};
object[] parametros = new object[] { Fechainicio, FechaFinal, empleadoid };
List
<DetalleDeduccionesEmpleado>
resultado = db1.Database.SqlQuery<DetalleDeduccionesEmpleado>
("exec sp_DetalleDeduccionesEmpleado @Fechainicio,@FechaFinal, @empleadoid", parametros).ToList();
List<DetalleDeduccionesEmpleadoTotal> total = db1.Database.SqlQuery<DetalleDeduccionesEmpleadoTotal>("exec sp_DetalleDeduccionesEmpleado @Fechainicio,@FechaFinal, @empleadoid", parametros).
ViewBag.deduc = resultado;''
Here I use it for the second time with some parameters of the previous one: SP1
SqlParameter inicio = new SqlParameter()
{
ParameterName = "@inicio",
DbType = DbType.DateTime,
Value = FechaDesde
};
SqlParameter final = new SqlParameter()
{
ParameterName = "@final",
DbType = DbType.DateTime,
Value = FechaHasta
};
SqlParameter areID = new SqlParameter()
{
ParameterName = "@areID",
DbType = DbType.String,
Value = Area.AreId
};
SqlParameter ageID = new SqlParameter()
{
ParameterName = "@ageID",
DbType = DbType.String,
Value = Agencia.AgeId
};
object[] parametros = new object[] { inicio, final, areID, ageID };
List
<DeduccionesNombre> NombreD = db.Database.SqlQuery<DeduccionesNombre>("exec sp_DeduccionesNombre @inicio,@final,@areID,@ageID", parametros).ToList();
ViewBag.duduccionesN = NombreD;