Avatar of arnololo123
arnololo123
 asked on

How to save a generated CSV file on server drive.

Hello , I am able to create a csv file but I don't know how to automatically save it on the server drive.
Here is the code that creates the CSV file

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("GIS_dasboard1"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {

                    cmd.Connection = con;
                    sda.SelectCommand = cmd;

                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        //Build the CSV file data as a Comma separated string.
                        string csv = string.Empty;
                        foreach (DataColumn column in dt.Columns)
                        {
                            csv += column.ColumnName + ';';
                        }

                        csv += "\r\n";
                        foreach (DataRow row in dt.Rows)
                        {
                            foreach (DataColumn column in dt.Columns)
                            {
                                csv += row[column.ColumnName].ToString().Replace(";", ",") + ';';
                            }
                            csv += "\r\n";
                        }
                        
                        
                        Response.Clear();
                        Response.Buffer = true;
                        Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv");
                        Response.Charset = "";
                        Response.ContentType = "application/text";
                        Response.Output.Write(csv);
                        Response.Flush();
                        Response.End();

                    }

                }
               }

        }

Open in new window

ASP.NETC#.NET Programming

Avatar of undefined
Last Comment
arnololo123

8/22/2022 - Mon
Michael Machie

in this snippet:
-------------------------
 Response.Clear();
                        Response.Buffer = true;
                        Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv")
---------------------------
AND
---------------------------
Response.Output.Write(csv);
---------------------------

I see the attachment being created but no location to send the attachment to.
arnololo123

ASKER
Yes this is the problem, I only want to save the file on the server hard drive and not even show it for download to the user.
ASKER CERTIFIED SOLUTION
arnololo123

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
arnololo123

ASKER
found the solution on my own
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23