ERROR: Another SqlParameterCollection already contains SqlParameter

0

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;
    
asked by Marco Eufragio 28.10.2018 в 01:13
source

1 answer

1

The problem is that you can not use the same parameters twice in this block:

List<DetalleDeduccionesEmpleado>resultado = db1.SqlQuery<DetalleDeduccionesEmpleado>
("exec sp_DetalleDeduccionesEmpleado @Fechainicio,@FechaFinal, @empleadoid",parametros).ToList();


List<DetalleDeduccionesEmpleadoTotal> total = db1.Database.SqlQuery<DetalleDeduccionesEmpleadoTotal>("exec 
sp_DetalleDeduccionesEmpleado @Fechainicio,@FechaFinal, @empleadoid", parametros).

That is, "total" wants to use the same "result" parameters, which is not valid. Try adding other parameters for that SP.

    
answered by 28.10.2018 / 01:54
source