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