someone knows how to perform excel with asp c # with store procedure with parameters [duplicate]

1

I am making a report with a stored procedure that has 3 entries in it, one is the amount, date, option, and on my page it has 3 input entries to enter and when you press export an excel file is downloaded, the problem is that it shows me an error "Error converting the value of the parameter from DropDownList to String". I would like you to help me please

  protected void ExportExcel(object sender, EventArgs e)
        {

            string constr = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
           string query = "COMISION_DIC16";
            //string query = "SELECT TOP 10 codigomoneda, idcredito, numerocredito FROM credito..credito;";
           // query += "SELECT TOP 10  idorigencredito, segmentotasa, segmentocartera FROM credito..credito;";

            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;

                    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";
                            ds.Tables[1].TableName = "numerocredito";


                            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();
                                }
                            }
                        }
                    }
                }
    
asked by PieroDev 03.01.2017 в 22:11
source

3 answers

1

Solution:

  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();
                            }
                        }
                    }
                }
            }
        }
    }
    
answered by 02.03.2017 / 01:24
source
1

As I understand the problem is to send the selected value

try to change this line

if you need the value of the dropdownlist

cmd.Parameters.Add("OPC", SqlDbType.VarChar, 30).Value = OPC.SelectedItem.Value;

if you need the text of the dropdownlist element

cmd.Parameters.Add("OPC", SqlDbType.VarChar, 30).Value = OPC.SelectedItem.Text;

You could be more specific with your error because the problem you are discussing is very different from the title of the question.

    
answered by 04.01.2017 в 00:14
1

This method, is used to add the parameter of a store procedure, review it by fa if you have any doubt you tell me. Maybe you can use it to pass the parameters directly, since you specify from the beginning what kind of data it is and the name of the field.

public void AgregarParametro(string NombreParametro, System.Data.SqlDbType TipoParametro, Object Valor)
{
    try
    {
        this.cmd.Parameters.Add(NombreParametro, TipoParametro).Value = Valor;
    }
    catch (Exception ex)
    {
        throw new ApplicationException("No se pudo agregar el parámetro. " + ex.Message.ToString().Trim());
    }
}
    
answered by 02.03.2017 в 01:32