Perhaps someone can help me here, I have a method that I am trying to accomplish two things

1. Retrieve a table from a stored procedure passing it one variable
2. Downloading that table in csv format

I am not sure why this method is not working and I am sure it is just a simple syntax error or my logic may be a little funky somewhere so any help would be appreciated.

protected void btn_export_Click(object sender, EventArgs e)
        {
            string filename = this.drpDealers.Text + "_mailfile";

            SqlConnection con = new SqlConnection(connectionString);
            DataTable tblMail = new DataTable();

            SqlCommand cmd = new SqlCommand("web_pull_mail_list", con) { CommandType = CommandType.StoredProcedure };
            cmd.Parameters.AddWithValue("@dealer_id", (Convert.ToInt32(this.drpDealers.Text)));

            cmd.Connection = con;

            con.Open();
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            adapt.Fill(tblMail);
            con.Close();


            HttpContext context = HttpContext.Current;
            context.Response.Clear();

            foreach (DataColumn column in tblMail.Columns)
            {
                context.Response.Write(column.ColumnName + ",");
            }

            context.Response.Write(Environment.NewLine);
            foreach (DataRow row in tblMail.Rows)
            {
                for (int i = 0; i < tblMail.Columns.Count; i++)
                {
                    context.Response.Write(row[i].ToString().Replace("," ,string.Empty) + ",");
                }
                context.Response.Write(Environment.NewLine);
            }

            context.Response.ContentType = "text/csv";
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
            context.Response.End();
        }

Thanks in advance for any help

Private Sub ConvertToExcel(ByRef r_dvReport As DataView)
    'first clean up the response.object
    Response.Clear()

    Response.Charset = ""

    'set the response mime type for excel
    Response.ContentType = "application/vnd.ms-excel"

    'create a string writer
    Dim stringWrite As New System.IO.StringWriter

    'create an htmltextwriter which uses the stringwriter
    Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)

    'instantiate a datagrid - used for output
    Dim dgExcelOutput As New DataGrid

    'set the output datagrid datasource to the dataset associated
    ' with the data grid being converted
    dgExcelOutput.DataSource = r_dvReport

    'bind the datagrid
    dgExcelOutput.DataBind()

    'tell the datagrid to render itself to htmltextwriter
    dgExcelOutput.RenderControl(htmlWrite)

    'all that's left is to output the html
    Response.Write(stringWrite.ToString)

    Response.End()
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.