Comma Delimited

Y

Yama

Hi,

I am looking to create a report comma delimited on a click of a button.

Explanantion:
1. Get from the database: "SELECT * FROM Customers WHERE Region = 'CA'"
2. Use either DataReader or DataSource
3. Create a button "Export"
4. On ServerClick Event prompt user to save as a text comma delimited file.

Can someone help me?

Yama
 
K

Ken Cox [Microsoft MVP]

Hi Yama,

Here's some code that will send a CSV file to Excel. Is that what you
needed?

Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
'Set the appropriate ContentType.
Dim filename As String = "orderdetails.csv"
Dim myCommand As New SqlCommand _
("select * from [order details] ", SqlConnection1)
myCommand.Connection.Open()
Dim myReader As SqlDataReader = _
myCommand.ExecuteReader _
(CommandBehavior.CloseConnection)
Dim i As Integer
Dim sb As New System.Text.StringBuilder
For i = 0 To myReader.FieldCount - 1
If i < (myReader.FieldCount - 1) Then
sb.Append(Chr(34) & myReader.GetName(i) & _
Chr(34) & ",")
Else
sb.Append(Chr(34) & myReader.GetName(i) & _
Chr(34) & vbCrLf)
End If
Next
While myReader.Read()
For i = 0 To myReader.FieldCount - 1
If i < (myReader.FieldCount - 1) Then
sb.Append(Chr(34) & _
myReader.GetValue(i).ToString & Chr(34) & ",")
Else
sb.Append(Chr(34) & _
myReader.GetValue(i).ToString & Chr(34) & vbCrLf)
End If
Next
End While

myReader.Close()
SqlConnection1.Close()
Response.ContentType = "Application/x-msexcel"
Response.AddHeader _
("content-disposition", "attachment; filename=""" & _
filename & """")
'Write the file directly to the HTTP output stream.
Response.Write(sb.ToString)
Response.End()
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top