1

Mi problema es el siguiente tengo checkboxList quiero recorrerlo y los valores seleccionados tomarlos para mi store procedure. Este codigo solo me toma un elemento, al momento que se selecciona 2 no me trae el datagridview

Les dejo mi codigo (store procedure y del visual)

DESDE YA MUCHAS GRACIAS

VISUAL

protected void cargarDatos()
    {

        int aux = 0;

        DataTable dtDatos = null;

        string strConsulta = "exec SP_Mostrar_ListaDeCuentasDeterminadas";

        if (!String.IsNullOrEmpty(txtFechaDesde.Text))
        {
            if (aux == 1)
            {
                strConsulta = strConsulta + ", @fechaDesde = '" + txtFechaDesde.Text + "'";
            }
            else
            {
                strConsulta = strConsulta + " @fechaDesde = '" + txtFechaDesde.Text + "'";
                aux = 1;
            }
        }
        if (!String.IsNullOrEmpty(txtFechaHasta.Text))
        {
            if (aux == 1)
            {
                strConsulta = strConsulta + ", @fechaHasta = '" + txtFechaHasta.Text + "'";
            }
            else
            {
                strConsulta = strConsulta + " @fechaHasta = '" + txtFechaHasta.Text + "'";
                aux = 1;
            }
        }

        if (cboComprobantes.SelectedItem.Value != "Seleccione")
        {
            if (aux == 1)
            {
                strConsulta = strConsulta + ", @comprobante = " + cboComprobantes.SelectedItem.Value;
            }
            else
            {
                strConsulta = strConsulta + " @comprobante = " + cboComprobantes.SelectedItem.Value;
                aux = 1;
            }
        }            


        for (int i = 0; i < chkCuentas.Items.Count; i++)
        {
            if (chkCuentas.Items[i].Selected)
            {

                    if (aux == 1)
                    {
                        strConsulta = strConsulta + ", @cuenta = " + chkCuentas.Items[i].Value;

                    }
                    else
                    {
                        strConsulta = strConsulta + " @cuenta = " + chkCuentas.Items[i].Value;
                        aux = 1;
                    }

            }
        }


        dtDatos = Conexion.llenarGridFromConsulta(strConsulta);

        grDatos.DataSource = dtDatos;
        grDatos.DataBind();
        btnExcel.Visible = true;
    }

STORE PROCEDURE

declare @consulta varchar(3000)
set @consulta = N' select TransaccionId, AccountCode, Cuentas.Description, NetAmount, CreatedDate, DocumentType, '
set @consulta = @consulta + ' DateName( day , EffectiveDate) + ' + '''  ''' + ' + DateName(  month , EffectiveDate) + ' + '''  ''' + ' + DateName(  year , EffectiveDate) as fecha, '
set @consulta = @consulta + ' JournalDescription, LineDescription, nombreApellido_usuario, cargo, Movimiento.Descripcion, TiposComprobante.codigo_comprobante '
set @consulta = @consulta + ' from TablaPrincipal '
set @consulta = @consulta + ' left join Cuentas on TablaPrincipal.AccountCode = Cuentas.id_cuenta '
set @consulta = @consulta + ' left join Usuarios on TablaPrincipal.UserId = Usuarios.Id_usuario '
set @consulta = @consulta + ' left join Movimiento on TablaPrincipal.Id_movimiento = Movimiento.Id_movimiento '
set @consulta = @consulta + ' left join TiposComprobante on TablaPrincipal.TipoComprobante = TiposComprobante.id_comprobante '

declare @aux int
set @aux = 0

    if @fechaDesde is not null 
    begin
        if @aux = 1
            begin
                set @consulta = @consulta + ' and CreatedDate >=  ''' +  CAST ( @fechaDesde as  varchar (12) ) + ''''

            end
        else
            begin
                set @aux = 1 
                set @consulta = @consulta + ' where CreatedDate >=   ''' +  CAST ( @fechaDesde as  varchar (12) )+ ''''
            end
    end 


    if @fechaHasta is not null 
    begin
        if @aux = 1
            begin
                set @consulta = @consulta + ' and CreatedDate <=  ''' +  CAST ( @fechaHasta as  varchar (12) )+ ''''

            end
        else
            begin
                set @aux = 1 
                set @consulta = @consulta + ' where CreatedDate <=  ''' +  CAST ( @fechaHasta as  varchar (12) )+ ''''
            end
    end 

         if @comprobante is not null 
    begin
        if @aux = 1
            begin
                set @consulta = @consulta + ' and TipoComprobante   =  ' +  CAST ( @comprobante as  varchar (8) )

            end
        else
            begin
                set @aux = 1 
                set @consulta = @consulta + ' where TipoComprobante  = ' +  CAST ( @comprobante as  varchar (8) )
            end
    end

         if @cuenta is not null 
    begin
        if @aux = 1
            begin
                set @consulta = @consulta + ' and AccountCode   =  ' +  CAST ( @cuenta as  varchar (8) )


            end
        else
            begin
                set @aux = 1 
                set @consulta = @consulta + ' where AccountCode  = ' +  CAST ( @cuenta as  varchar (8) )


            end
    end

1 respuesta 1

2

El error es de concepto y esta en el SP...

Si vos escribis un where de la siguiente forma:

Where AccountCode = 1 and AccountCode = 2 and AccountCode = 3

ningun registro cumple con esa condición.

La base de datos procesa y revisa los registros uno a uno, y de esta forma esta buscando en cada fila, que el AccountCode sea igual a 1 y a 2 y a 3 al mismo tiempo, cosa que es imposible.

Lo que a vos te tiene que quedar, es algo asi:

Where AccountCode in (1,2,3)

Porque la clausula in, es como un or.

Otra solución mas fea seria usar el or

Where ...otras clausulas... and (AccountCode = 1 or AccountCode = 2 or AccountCode = 3)

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.