15
Jul

Export Sql Table Data in to CSV File in asp .net

   Posted by: admin   in .Net Emperor

Hello Friends,

Sorry after so long time i m writing this post recently i was working on few bigger task in asp .net.

now days i feel on internet when we goggling for the CSV (Comma Separated Values) file related data so you will find not as much interesting data. so i decide to write two post for the CSV functionality with asp .net.

this article gives you way how to export the sql server tables data in to the CSV file so please read it carefully and apply it in your web application.

please find the following code in your export event.

Sub ExportCsv()
Response.Clear()
Response.Buffer = True
Response.AddHeader(”content-disposition”, “attachment;filename=GridViewExport.csv”)
Response.Charset = “”
Response.ContentType = “application/text”

Dim sb As New StringBuilder()
sb.Append(”Name” + “,”c)
sb.Append(”Company” + “,”c)
sb.Append(”Address” + “,”c)
sb.Append(”ZIPCode” + “,”c)
sb.Append(”City” + “,”c)
sb.Append(”State” + “,”c)
sb.Append(”Country” + “,”c)
sb.Append(”Telephone” + “,”c)
sb.Append(”Email” + “,”c)
sb.Append(vbCr & vbLf)

Dim cn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(”testDatabase”).ConnectionString.ToString())
Dim adp As SqlDataAdapter = New SqlDataAdapter(”select * from users where userid=1;”, cn)
Dim ds As New DataSet
adp.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
sb.Append(ds.Tables(0).Rows(0)(”Name”) + “,”c)
sb.Append(ds.Tables(0).Rows(0)(”Company”) + “,”c)
sb.Append(ds.Tables(0).Rows(0)(”Address”) + “,”c)
sb.Append(ds.Tables(0).Rows(0)(”ZIPCode”) + “,”c)
sb.Append(ds.Tables(0).Rows(0)(”City”) + “,”c)
sb.Append(ds.Tables(0).Rows(0)(”State”) + “,”c)
sb.Append(ds.Tables(0).Rows(0)(”Country”) + “,”c)
sb.Append(ds.Tables(0).Rows(0)(”Telephone”) + “,”c)
sb.Append(ds.Tables(0).Rows(0)(”Email”) + “,”c)
End If

sb.Append(vbCr & vbLf)
Response.Output.Write(sb.ToString())
Response.Flush()
Response.End()
End Sub

Enjoy Friends please let me know if you have any kind of problem in above code also please let me if you have any kind of suggestion for the site or code all your suggestion and comments are welcome on this websites.

Tags:

This entry was posted on Wednesday, July 15th, 2009 at 12:34 am and is filed under .Net Emperor. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 comments so far

imtiaz
 1 

When I try to export data which is in Arabic, it is giving junk values, can you suggest what could be the reason for this.
If the data is in English it is working fine but for Arabic it is giving junk data.
I tried to export in excel it is working fine.
what could be issue with csv format.

Thanks

September 27th, 2009 at 4:29 am
Jick
 2 

please let me know C# code

November 6th, 2009 at 4:22 am
daxesh
 3 

Dear sir,
plz send me some code of how to export multiple sqltable data in single excelfile in c#.net

January 19th, 2010 at 4:46 am

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment