Bascically, on the page you create a button and on the Button Click event you add following code :

 

protected void Button2_Click(object sender, EventArgs e)
    {
        //create file for end user
 string attachment = "attachment; filename=<whateveryouwantnameit>.csv";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "text/csv";
        HttpContext.Current.Response.AddHeader("Pragma", "public");

 //create string builder to buffer the data later then transfer it to file
        var sb = new StringBuilder();

        SqlConnection myConnection = new SqlConnection("user id=<UserName>;" +
                                      "password=<PW>;server=<ServerName>;" +
                                      "database=<DBName>; " +
                                      "connection timeout=30");
        myConnection.Open();
        SqlCommand command = new SqlCommand("Select ******* ", myConnection);

// Creates a SqlDataReader instance to read data from the table.
        SqlDataReader dataReader = command.ExecuteReader();

//Create a datatable to load from dataReader
        DataTable dt = new DataTable();
        dt.Load(dataReader);

//Pass content to Datatable to string stream
        string[] columnNames = dt.Columns.Cast<DataColumn>().
                                  Select(column => column.ColumnName).
                                  ToArray();
        sb.AppendLine(string.Join(",", columnNames));

        foreach (DataRow row in dt.Rows)
        {
            string[] fields = row.ItemArray.Select(field => field.ToString()).
                                            ToArray();
            sb.AppendLine(string.Join(",", fields));
        }


 //pass string stream to CSV file
        HttpContext.Current.Response.Write(sb.ToString());
        HttpContext.Current.Response.End()
    }

 

 

This is a web applicatio I created for one of my colleague, since he always need this information and it need to be up to date, so I created this web applicaiton for him so he can always go to the website and get it.

 

 

About Lei

I am an IT specialist with over 10 year experience - years on Automation, on-Premise or Azure.

I am happy to develop however never want be a full time developer. Only do what I have to do. If it has to be PowerShell,HTML, PHP, CSS, C#, VBS or JS, front end or backend, so be it, doesn't matter!

Spent years with Windows, SCCM, SharePoint, SQL and Exchange servers. For last several years, I have been actively working under On Premise > Azure environment.

THERE IS NO WAY BACK!!!

Current Certificates:
    Microsoft® Certified-
  • -Enterprise Administrator
  • -Database Administrator
  • -SharePoint Administrator
  • -Administering and Deploying SCCM 2012
Red Hat Certified Technician
ITIL V3 Foundation - Practitioner

Working on Azure Certificates now and hopefully they can stop upgrading their questions one day! GIVE ME A BREAK!

Contact Lei

Name *
Email *
Comments *

Traffic since 10/08/2016

Today53
Yesterday83
This week677
This month1973
Total416333

Visitor Info

  • IP: 54.227.51.103
  • Browser: Unknown
  • Browser Version:
  • Operating System: Unknown

Who Is Online

1
Online

2017-12-17

Login