Code Snipit for saving to CSV

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Code Snipit for saving to CSV

    Can some one help me find a code snipit for saving the contents of a
    data set to a comma seperated file?

    I have created a web service in asp.net that will read the data from a
    AS/400 DB2 database via a OleDB connection. Then I create the
    dataset, Now what do I do
    Here is the function now
    ------------------------------------------------
    Public Function GetTicketData() As DataSet
    'Public Function GetTicketData(ByVal varbpno, ByVal
    varAcctperdid) As DataSet
    Dim RecLocName As String
    Dim AuthNO As String
    Dim TktRecDt As Date
    Dim RcdVolQT As String
    Dim RcdCullQT As String
    Dim SpProd As String

    REM -- get the data from the database
    Dim sqlText As String = "select reclocnm, a.authno, tktrecdt,
    rcdvolqt, rcdcullqt, sppdfmgd from "
    sqlText = sqlText + " ticket a, tktdtl b, recloc c, spprod d ,
    auth e where a.tktid = b.tktid and a.acctperdid = b.acctperdid"
    sqlText = sqlText + " and a.reclocid = c.reclocid and
    b.spprodid = d.spprodid and a.authno = e.authno"
    sqlText = sqlText + " and e.splrbpno = 1 and a.acctperdid =
    222"
    'sqlText = sqlText + " and e.splrbpno = " + varbpno + " and
    a.acctperdid = " + varAcctperdid
    Dim dbRead As OleDb.OleDbDataReader = GetDataReader(sqlText)

    REM -- create the datatable
    Dim ds As DataSet = New DataSet("TicketInfo")
    Dim dt As DataTable = ds.Tables.Add("TicketData")
    Dim dr As DataRow

    REM -- create the columns in the datatable
    dt.Columns.Add(New DataColumn("RecLocName",
    System.Type.GetType("System.String")))
    dt.Columns.Add(New DataColumn("AuthNO",
    System.Type.GetType("System.String")))
    dt.Columns.Add(New DataColumn("TktRecDt",
    System.Type.GetType("System.String")))
    dt.Columns.Add(New DataColumn("RcdVolQT",
    System.Type.GetType("System.String")))
    dt.Columns.Add(New DataColumn("RcdCullQT",
    System.Type.GetType("System.String")))
    dt.Columns.Add(New DataColumn("SpProd",
    System.Type.GetType("System.String")))

    While dbRead.Read()
    RecLocName = dbRead.Item("RecLocNM").ToString()
    AuthNO = dbRead.Item("AuthNO").ToString()
    TktRecDt =
    DateTime.Parse(dbRead.Item("TktRecDT").ToString())
    RcdVolQT = dbRead.Item("RcdVolQT").ToString()
    RcdCullQT = dbRead.Item("RcdCullQT").ToString()
    SpProd = dbRead.Item("SPPDFMGD").ToString()

    REM -- Add to DataSet ds
    dr = dt.NewRow()
    dt.Rows.Add(dr)
    End While

    'Return ds




    End Function
    -------------------------------------------------------------
    Thanks for any help.
    Steven Thomas Guest

  2. Similar Questions and Discussions

    1. Why doesn't the Code Completion occur in FlexBuilder IDEwhen source code is in an external file?
      I am seperating my .as from the MXML by using the following in my file.mxml: <mx:Script source="file.as"> When I edit file.as, the code...
    2. saving XML code
      Hi, I want to save XML code into my database using php. is using: $xml = htmlentities($str, ENT_QUOTES); and saving the converted code a...
    3. Saving Images While Saving ASP Pages !
      Dear Friends Hope you all doing great. I have this problem. When I try to save any ASP page, I get the message that "The page will not save...
    4. Security problem with Managed Code calling Unmanaged Code in a Web Page
      Hello, I have a web page which contains an ActiveX control (unmanaged) and a Windows Forms User Control (managed). Both reside on a web page and...
    5. Dreamweaver MX not saving code properly
      Cheap and nasty solution: - Do a global search and replace :) MX is like a box of chocolates - Someone's usually eaten the good bits already!
  3. #2

    Default Re: Code Snipit for saving to CSV

    Here's some code that might give you some ideas...


    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    '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.CloseConne ction)
    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

    "Steven Thomas" <stmthoma@gapac.com> wrote in message
    news:613e1cae.0306260843.2bf4f921@posting.google.c om...
    Can some one help me find a code snipit for saving the contents of a
    data set to a comma seperated file?

    I have created a web service in asp.net that will read the data from a
    AS/400 DB2 database via a OleDB connection. Then I create the
    dataset, Now what do I do
    Here is the function now
    ------------------------------------------------
    Public Function GetTicketData() As DataSet
    'Public Function GetTicketData(ByVal varbpno, ByVal
    varAcctperdid) As DataSet
    Dim RecLocName As String
    Dim AuthNO As String
    Dim TktRecDt As Date
    Dim RcdVolQT As String
    Dim RcdCullQT As String
    Dim SpProd As String

    REM -- get the data from the database
    Dim sqlText As String = "select reclocnm, a.authno, tktrecdt,
    rcdvolqt, rcdcullqt, sppdfmgd from "
    sqlText = sqlText + " ticket a, tktdtl b, recloc c, spprod d ,
    auth e where a.tktid = b.tktid and a.acctperdid = b.acctperdid"
    sqlText = sqlText + " and a.reclocid = c.reclocid and
    b.spprodid = d.spprodid and a.authno = e.authno"
    sqlText = sqlText + " and e.splrbpno = 1 and a.acctperdid =
    222"
    'sqlText = sqlText + " and e.splrbpno = " + varbpno + " and
    a.acctperdid = " + varAcctperdid
    Dim dbRead As OleDb.OleDbDataReader = GetDataReader(sqlText)

    REM -- create the datatable
    Dim ds As DataSet = New DataSet("TicketInfo")
    Dim dt As DataTable = ds.Tables.Add("TicketData")
    Dim dr As DataRow

    REM -- create the columns in the datatable
    dt.Columns.Add(New DataColumn("RecLocName",
    System.Type.GetType("System.String")))
    dt.Columns.Add(New DataColumn("AuthNO",
    System.Type.GetType("System.String")))
    dt.Columns.Add(New DataColumn("TktRecDt",
    System.Type.GetType("System.String")))
    dt.Columns.Add(New DataColumn("RcdVolQT",
    System.Type.GetType("System.String")))
    dt.Columns.Add(New DataColumn("RcdCullQT",
    System.Type.GetType("System.String")))
    dt.Columns.Add(New DataColumn("SpProd",
    System.Type.GetType("System.String")))

    While dbRead.Read()
    RecLocName = dbRead.Item("RecLocNM").ToString()
    AuthNO = dbRead.Item("AuthNO").ToString()
    TktRecDt =
    DateTime.Parse(dbRead.Item("TktRecDT").ToString())
    RcdVolQT = dbRead.Item("RcdVolQT").ToString()
    RcdCullQT = dbRead.Item("RcdCullQT").ToString()
    SpProd = dbRead.Item("SPPDFMGD").ToString()

    REM -- Add to DataSet ds
    dr = dt.NewRow()
    dt.Rows.Add(dr)
    End While

    'Return ds




    End Function
    -------------------------------------------------------------
    Thanks for any help.


    Ken Cox [Microsoft MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139