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;

            SqlDataAdapter adapt = new SqlDataAdapter(cmd);

            HttpContext context = HttpContext.Current;

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

            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.ContentType = "text/csv";
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");

Thanks in advance for any help

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

    Response.Charset = ""

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

    '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

    'tell the datagrid to render itself to htmltextwriter

    'all that's left is to output the html

End Sub
This article has been dead for over six months. Start a new discussion instead.