Comma Delimited

Discussion in 'ASP .Net' started by Yama, Dec 15, 2003.

  1. Yama

    Yama Guest

    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
     
    Yama, Dec 15, 2003
    #1
    1. Advertising

  2. 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

    "Yama" <> wrote in message
    news:...
    > 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
    >
    >
     
    Ken Cox [Microsoft MVP], Dec 16, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia

    Read Comma Delimited File

    Luis Esteban Valencia, Jul 27, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,812
    Paul Clement
    Jul 27, 2005
  2. Edward A Thompson

    JavaMail MimeMessage - comma delimited?

    Edward A Thompson, Oct 8, 2003, in forum: Java
    Replies:
    9
    Views:
    5,633
    GaryM
    Oct 10, 2003
  3. ouioui
    Replies:
    1
    Views:
    1,634
    Mukul Gandhi
    Sep 23, 2004
  4. Replies:
    2
    Views:
    707
    David Carlisle
    Feb 18, 2005
  5. RyanL
    Replies:
    6
    Views:
    691
    Paul McGuire
    Aug 28, 2007
Loading...

Share This Page