using System; using System.Configuration; using System.Data; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnDownload_Click(object sender, EventArgs e) { string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("SELECT * FROM UserTable")) { 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) { //Add the Header row for CSV file. csv += column.ColumnName + ','; } //Add new line. csv += "\r\n"; foreach (DataRow row in dt.Rows) { foreach (DataColumn column in dt.Columns) { //Add the Data rows. csv += row[column.ColumnName].ToString().Replace(",", ";") + ','; } //Add new line. csv += "\r\n"; } //Download the CSV file. Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=SqlDataInCSVFile.csv"); Response.Charset = ""; Response.ContentType = "application/text"; Response.Output.Write(csv); Response.Flush(); Response.End(); } } } } } }
ASP.NET, ASP.NET Core, JavaScript, SQL Server,WCF Services, Web Services, Angular, AngularJS, Ionic Framework
Navigation
How to download/upload file with Dropbox API
Custom Search in jquery.dataTables.js
ASP.NET Core-Implementing the Singleton Pattern.
ASP.NET Core-Token Based Authorization.
Create a Web API with ASP.NET Core.
ASP.NET Routing(URL Rewriting ) with the help of Global.asax.
Configuration of WebApiConfig to call Post Method.
Create a nested list of items from DataTable.
Basic Authentication in ASP.NET Web API.
Creating Documents by Using the Open XML Format SDK 2.0 in VS2012.
Encryption and Decryption of image file in C#.
Receive Notifications on Record Change with SQLTableDependency.
Retrieving/Uploading image using ASP.NET Web API.
Calling a Web API Through C# (Console Application)
How to use SqlXml in C# for Bulk Uploading.
Create Window C# Application Using Mongo Database.
Generate and download the .csv file.
Reading CSV file data into a DataTable.
Transferring Data Using SqlBulkCopy in C#.
Invoke Method Asynchronous By Delegate.
CUSTOM Paging In GridView in C#.
Transferring Data Using SqlBulkCopy in C#.
USE OF SqlXml in C#.
Convert a DataTable into String XML in C#.
Read Excel File into DataSet in ASP.NET Using C#.
Insert element into xml document and pass to database by SqlXml.
Generics Support Inheritance.
Onrowcommand In GridView.
Bind ASP Dropdown by JSON.
Generics Support Inheritance in C#.
Generics Support Inheritance in C#.
Call javascript function from code behind in C#.
ASP.Net GridView - Get Row Index on RowCommand.
ASP.NET ListView : Find Value of DataSet In ItemDataBound.
An autocomplete/fuzzy search in textbox.
Web Browser Push-notifications.
Convertingan file to base64 in angular 7.
Angular6 {pipe} - How to format a phone number and subStr string.
Angular 6: Add a Http Authentication Bearer token.
Angular 6: Insert text at cursor position in angular-editor
Add,Remove and Validate FormGroup dynamically to FormArray.
How to add server side pagination in Angular 6 for a table?.
Creating custom filter pipe for order by date,number and text on *ngFor.
Angular 2 Quickstart with Visual Studio 2017.
Set Dropdown Default Value Using AngularJS.
Multiselect - JQuery For Dropdown Box With Checkboxes.
Create Directive to Allow Decimal Numbers and Number Using AngularJS.
SQL Server Real-Time update on Record Change with SignalR and SQLDependency with AngularJS.
Difference between Service vs Provider vs Factory.
Passing Parameters without Showing Them in State URLs.
Basic HTTP Authentication.
Upload File using file Handler.ashx.
Ionic Capacitor: Get Current Position and Display Driving Route Using Google Maps and Geolocation
Send Ionic Push Notification to Android App by C# using FCM.
Android - Get the Google Services Json
Push Notifications with Ionic 6 and Firebase
Read and Write Data on Android with Firebase Realtime Database
Get started Ionic Framework with Node JS
Gradle Error: build for android
Push Notifications using Firebase Cloud Messaging FCM in C#
Web.Config File setting on AJAX Enabled WCF Service
Transport layer security implementation.
Monday, 8 August 2016
Generate and download the .csv file.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment