1

Estoy realizando un reporte con un procedimiento almacenado que tiene como entradas 3 entras uno es el monto, fecha,opcion.y en mi pagina tiene 3 input entradas para que ingrese y cuando presione exportar se baje un archivo excel el problema es que me muestra un error "COMO LA IMAGEN PORFAVOR introducir la descripción de la imagen aquí. Quisiera que me ayuden Porfavor

 protected void ExportExcel(object sender, EventArgs e)
        {

            string constr = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
           string query = "COMISION_DIC16";


            using (SqlConnection con = new SqlConnection(constr))
            {


                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("TP", SqlDbType.VarChar, 30).Value = txtTC.Text;
                    cmd.Parameters.Add("Fecha_FDM", SqlDbType.VarChar, 30).Value = txtFecha_FDM.Text;
                    cmd.Parameters.Add("OPC", SqlDbType.VarChar, 30).Value = OPC.SelectedItem.Value;


                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;


                        sda.SelectCommand = cmd;


                        using (DataSet ds = new DataSet())
                        {
                            sda.Fill(ds);

                            //Set Name of DataTables.
                            ds.Tables[0].TableName = "idcredito";
                            [![introducir la descripción de la imagen aquí][1]][1]



                            using (XLWorkbook wb = new XLWorkbook())
                            {
                                foreach (DataTable dt in ds.Tables)
                                {
                                    //Add DataTable as Worksheet.
                                    wb.Worksheets.Add(dt);
                                }

                                //Export the Excel file.
                                Response.Clear();
                                Response.Buffer = true;
                                Response.Charset = "";
                                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                                Response.AddHeader("content-disposition", "attachment;filename=Cuadro_1_dic16.xlsx");
                                using (MemoryStream MyMemoryStream = new MemoryStream())
                                {
                                    wb.SaveAs(MyMemoryStream);
                                    MyMemoryStream.WriteTo(Response.OutputStream);
                                    Response.Flush();
                                    Response.End();
                                }
                            }
                        }
                    }
                }
10
  • Aparentemente el stored procedure COMISION_DIC16 no existe. ¿Seguro escribistes bien el nombre del stored procedure?
    – sstan
    el 4 ene. 2017 a las 13:41
  • @sstan Muchas gracias por responder si lo tengo creado pero como viste la imagen me vota de color rojo sda.fill(ds) porque cuando solo un select prueba from prueba en ves del sp y en ves de los parametros si me sale pero con el store procedure me vota ese error
    – PieroDev
    el 4 ene. 2017 a las 13:47
  • Verifica también que el usuario que estas usando en la cadena de conexión tenga permisos para ejecutar el procedimiento almacenado dentro de SQL Server. el 4 ene. 2017 a las 13:59
  • ¿Seguro que el stored procedure no está definido en un esquema distinto a dbo? ¿Seguro estás apuntando al database correcto? Si quieres, como prueba, puedes cambiar el CommandType a Text y usar la forma más completa y explícita de ejecutar el stored procedure: string query = "exec COMISION_DIC16 @TP, @Fecha_FDM, @OPC" y ver que pasa. ¿Puedes ejecutar el stored procedure manualmente en SSMS y asegurarte que funciona bien desde allí?
    – sstan
    el 4 ene. 2017 a las 14:00
  • Como nota aparte...este código no respeta la arquitectura MVC...porque estás mezclando base de datos, modelo, vista todo en un solo método. el 4 ene. 2017 a las 14:07

1 respuesta 1

0

Solucion :

  protected void ExportExcel(object sender, EventArgs e)
    {

        string query = "Rpt_Refinanciado"; //"COMISION"

        using (  SqlConnection con = new SqlConnection(ObtenerCadenaConexion()))
        {


            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.CommandTimeout = 90000000;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("f_ini", SqlDbType.VarChar, 50).Value = txtFecha_Ini.Text;
            cmd.Parameters.Add("f_fin", SqlDbType.VarChar, 50).Value = txt_Fecha_Fin.Text;
             // cmd.Parameters.Add("OPC", SqlDbType.VarChar, 50).Value = OPC.SelectedItem.Value;



                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;


                    sda.SelectCommand = cmd;

                    using (DataSet ds = new DataSet())
                    {
                        sda.Fill(ds);

                        //Set Name of DataTables.
                        ds.Tables[0].TableName = "Rpt-Comision";



                        using (XLWorkbook wb = new XLWorkbook())
                        {
                            foreach (DataTable dt in ds.Tables)
                            {
                                //Add DataTable as Worksheet.
                                wb.Worksheets.Add(dt);
                            }

                            //Export the Excel file.
                            Response.Clear();
                            Response.Buffer = true;
                            Response.Charset = "";
                            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            Response.AddHeader("content-disposition", "attachment;filename=Reporte_Comision.xlsx");

                            using (MemoryStream MyMemoryStream = new MemoryStream())
                            {
                                wb.SaveAs(MyMemoryStream);
                                MyMemoryStream.WriteTo(Response.OutputStream);
                                Response.Flush();
                                Response.End();
                            }
                        }
                    }
                }
            }
        }
    }

Tu Respuesta

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

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