Asked by:
Trying to generate multiplke csv files in one click event

Question
-
User1016820894 posted
Is it possible to create multiple csv files in memory stream and prompt the user to open/save each one in one click event? Here is the code behind I have. I call the same sub-routine twice to generate 2 csv files. When I test it, the first call goes through successfully and prompts the user for the first file (res,csv) but after I choose open/save and complete that process nothing happens. I tried debugging and the code just seems to stop. It never returns or makes the second call to the sub-routine. What am I missing (assuming it can be done).
protected void CSVFiles_Click(object sender, EventArgs e)
{
CreateCSVFile("res", Date.Text);
CreateCSVFile("staff", Date.Text);
}
protected void CreateCSVFile(string datatype, string StDT)
{using (SqlDataAdapter da = new SqlDataAdapter())
{
try
{
SqlConnection connsql = new SqlConnection("integrated security=SSPI;data source=sqldb;persist security info=False;initial catalog=MiscData;MultipleActiveResultSets=true;Connect Timeout=90");
connsql.Open();
da.SelectCommand = new SqlCommand("SP_CSVFile", connsql);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.CommandTimeout = 120;
da.SelectCommand.Parameters.Add(new SqlParameter("@DataType", datatype));
da.SelectCommand.Parameters.Add(new SqlParameter("@ReportDate", StDt));SqlDataReader myReader = null;
myReader = da.SelectCommand.ExecuteReader();try
{
MemoryStream memoryStream = new MemoryStream();
{
using (System.IO.StreamWriter fs = new System.IO.StreamWriter(memoryStream))
{
// Loop through the fields and add headers
for (int i = 0; i < myReader.FieldCount; i++)
{
string name = myReader.GetName(i);
if (name.Contains(","))
{
name = "\"" + name + "\"";
}if (i < myReader.FieldCount - 1)
{
fs.Write(name + ",");
}
else
{
fs.Write(name);
}}
fs.WriteLine();// Loop through the rows and output the data
while (myReader.Read())
{
for (int i = 0; i < myReader.FieldCount; i++)
{
string value = myReader[i].ToString();
if (i == 1)
{
value = value.Replace(" 12:00:00 AM", "");
}
if (value.Contains(","))
{//if (value.Contains(",") || value.Contains("\"\"") || value.Contains("\"\r\"") || value.Contains("\"\n\"") || value.Contains("\"''\""))
value = "\"" + value.Replace("\"", "'") + "\"";
}
else if (value == "\"\n\r\"")
//fs.Write(value.Join(",", value.Select(s => s == null ? null : ("\"" + s.Replace("\"", "\"\"") + "\""))) + "\r\n");
{
value = "";
}
else
{
//value = "\"" + value.Replace("\"", "'") + "\"";
}if (i < myReader.FieldCount - 1)
{
fs.Write(value + ",");
}
else
{
fs.Write(value);
}}
fs.WriteLine();}
fs.Flush();
memoryStream.Flush();
}string fileName = datatype + ".csv";
Response.Clear();
Response.Buffer = false;
Response.AddHeader("content-disposition", string.Format("attachment; filename=" + fileName));
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(memoryStream.ToArray());
Response.Flush();
Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event
Response.Cache.SetCacheability(HttpCacheability.NoCache);
}
}
catch (Exception ex)
{
}
connsql.Close();}
catch (Exception ex)
{
}
}
}
Monday, May 11, 2020 6:37 PM
All replies
-
User475983607 posted
baldwinjohn
Is it possible to create multiple csv files in memory stream and prompt the user to open/save each one in one click event?No. There's one request and one response. You can zip the files and let the user download the zipped files.
Monday, May 11, 2020 6:40 PM -
User1016820894 posted
How would I do that with the code above? I am not real proficient in doing this.
Thanks.
Monday, May 11, 2020 6:43 PM