Question Problem writing data to an ascii format.

patharte

New member
Joined
Feb 16, 2010
Messages
2
Programming Experience
10+
Hi guys. I have an issue that's been on my desk now for a while and it's being quite a pain. I have data that has to be collected from my database and sent to a supplier's edi system. The file has to be in ascii format so their system can read it. I am writing what I see as an ascii format file but they say it's in edibc format when they receive it. I am posting my code and hopefully somebody here can help me out. Funny thing is that I have done this before successfully and am using virtually the same code but to no avail!! Anyway, here's the code...there might be multiple mistakes here by now as I have been picking away at it all day!!


VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim filed As String = "Y"

        ' get records to be transmitted and plop them into an array list
        Dim con As VICSClaimDAO = New VICSClaimDAOSQL()
        Dim selectedRecords As List(Of WayFileRecord) = con.getClaimsFromMaster(filed)
        Dim noOfArrayElements As Integer = selectedRecords.Count

        Dim intDate As String = Convert.ToDateTime(Date.Now).ToString("yyMMdd")
        Dim intTime As String = Format(DateTime.Now, "hhmm")
        ' Segment terminator(unprintable)
        Dim ST As Byte = Convert.ToByte(&H1C, 16)   'Unprintable
        'Element separator
        Dim ES As Byte = Convert.ToByte(&H7E, 10)   ' Tilde ~
        ' Sub Element Separator
        Dim SES As Byte = Convert.ToByte(&H3C)      ' Less Than <

        ' Get the data interchange control number
        Dim DICNo As Integer
        Dim myConnection As New SqlConnection
        Dim x As New Global.CreateVICSFile.Model.DataInterContNumber
        myConnection = New SqlConnection("Data Source=server\;Initial Catalog=databasename;Integrated Security=True")
        myConnection.Open()
        Dim cmdGetD As New SqlCommand("getDICN", myConnection)
        cmdGetD.CommandType = CommandType.StoredProcedure
        cmdGetD.Parameters.Add(New SqlParameter("@DICN", DICNo))
        Dim readerD As SqlDataReader = Nothing
        readerD = cmdGetD.ExecuteReader(CommandBehavior.CloseConnection)
        If readerD.Read() Then
            x.DICN = readerD.GetInt32(readerD.GetOrdinal("DICN"))
        End If
        readerD.Close()
        myConnection.Close()
        ' Increment number for this transaction
        DICNo = x.DICN + 1

        ' Create the file and file name to be transmitted
        Dim fileDate As String = Format(DateTime.Now, "yyyyMMddhhmm")
        Dim FILENAME As String = "C:\test" + fileDate
        Dim objWriter As New System.IO.StreamWriter(FILENAME, False, System.Text.Encoding.ASCII)

        Dim IDLine As String
        Dim SELine As String
        Dim GELine As String

        Dim ISALine As String = Trim("ISA" + Chr("&H7E") + "00" + Chr("&H7E") + "          " + Chr("&H7E") + "          " + Chr("&H7E") + "ZZ" + Chr("&H7E") + "VDICS          " + Chr("&H7E") + intDate + Chr("&H7E") + intTime + Chr("&H7E") + "U" + Chr("&H7E") + "00302" + Chr("&H7E") + noOfArrayElements.ToString + Chr("&H7E") + "0" + Chr("&H7E") + "P" + Chr("&H7E") + Chr("&H3C"))
        objWriter.Write(ISALine)

        Dim GSLine As String = "GS" + Chr("&H7E") + "GC" + Chr("&H7E") + "44552" + Chr("&H7E") + "VDICS" + Chr("&H7E") + intDate + Chr("&H7E") + intTime + Chr("&H7E") + DICNo.ToString.PadLeft(9, "0") + Chr("&H7E") + "X" + Chr("&H7E") + "003020" + Chr("&H1C")
        objWriter.Write(GSLine)

        Dim STLine As String = "ST" + Chr("&H7E") + "924" + Chr("&H7E") + noOfArrayElements.ToString + Chr("&H1C")
        objWriter.Write(STLine)

        Dim BIXLine As String = "BIX" + Chr("&H7E") + "02" + Chr("&H7E") + "XHRV" + Chr("&H7E") + intDate + Chr("&H7E") + "04" + Chr("&H1C")
        objWriter.Write(BIXLine)

        For i = 0 To selectedRecords.Count - 1
            Dim something As Integer = i
            Dim v = selectedRecords.Item(i).VinNo
            Dim clm As VICSClaimDAO = New VICSClaimDAOSQL()
            Dim selectedClaims As List(Of VICSClaim) = con.getDamages(v)
            Dim claimsPerVin As Integer = selectedClaims.Count

            Dim VCLine As String = "VC" + Chr("&H7E") + Trim(selectedRecords.Item(i).VinNo) + Chr("&H1C")
            objWriter.Write(VCLine)

            For Y = 0 To selectedClaims.Count - 1
                IDLine = "ID" + Chr("&H7E") + Trim(selectedClaims.Item(Y).AreaCode) + Chr("&H7E") + Trim(selectedClaims.Item(Y).TypeCode) + Chr("&H7E") + Trim(selectedClaims.Item(Y).SeverityCode) + Chr("&H1C")
                objWriter.Write(IDLine)
            Next
        Next

        SELine = "SE" + Chr("&H7E") + noOfArrayElements.ToString + Chr("&H7E") + DICNo.ToString.PadLeft(9, "0") + Chr("&H1C")
        objWriter.Write(SELine)
        GELine = "GE" + Chr("&H7E") + noOfArrayElements.ToString + Chr("&H7E") + DICNo.ToString.PadLeft(9, "0") + Chr("&H1C")
        objWriter.Write(GELine)
        Dim IEALine As String = "IEA" + Chr("&H7E") + "01" + DICNo.ToString.PadLeft(9, "0") + Chr("&H1C")
        objWriter.Write(IEALine)

        objWriter.Close()
        MsgBox("Done with Start ASCII")

    End Sub
HEres' the out put:
ISA~00~ ~ ~ZZ~TVICS ~100215~0938~U~00302~5~0~P~<GS~GC~99999~TVICS~100215~0938~000000052~X~003020ST~924~5BIX~02~XXXX~100215~04VC~1C8F4N8973T538128ID~51~00~00VC~5C8F4N8996T534971ID~51~00~00VC~CT5HB1R83YQ02582ID~10~14~01VC~1W9GHN2712U5R7598ID~57~12~01VC~1QRGHN3B23YQE4197ID~51~00~00SE~5~000000052GE~5~000000052IEA~01000000052
 
Last edited by a moderator:
Back
Top