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.
Hello ! I am Arjun Jadeja a Software Engineer by Profession. You can contribute and I will distribute your ideas through this site. Thanks and Enjoy
3 comments so far
Leave a reply