14 Replies - 553 Views - Last Post: 29 November 2018 - 08:30 PM Rate Topic: -----

#1 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 19-November 18

Reading DAT file

Posted 19 November 2018 - 06:02 PM

Hi All,

I am wondering does anyone know of any good tutorial on using dat files e.g. reading or formatting in an array.

Here is the dat file contents:
_id,itemcode,location
1,RT006,16,A223
2,RT003,13,A223,
3,RT001,11,A223,

I create a table array in the vb.net form. I want to load some column values from the dat file and then import it into database table. The column values range from _id to Location. The column titles will not be loaded. Any help would be great.
thanks

Regards,
Steve

Is This A Good Question/Topic? 0
  • +

Replies To: Reading DAT file

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14581
  • View blog
  • Posts: 58,445
  • Joined: 12-June 08

Re: Reading DAT file

Posted 19 November 2018 - 06:33 PM

Stream reader would work fine into a dataset.
Was This Post Helpful? 1
  • +
  • -

#3 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 19-November 18

Re: Reading DAT file

Posted 19 November 2018 - 06:44 PM

View Postmodi123_1, on 19 November 2018 - 06:33 PM, said:

Stream reader would work fine into a dataset.



Here I attached the dat file contents I want to import. I just want to import the second line until the rests. the first line is the column titles.
Could you elaborate more detail ? I am sure if streamreader can separate column titles and column values.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14581
  • View blog
  • Posts: 58,445
  • Joined: 12-June 08

Re: Reading DAT file

Posted 19 November 2018 - 06:58 PM

You can split a string with 'split' around a comma.

Stream read with a while loop, string split into am array, add to a table.
Was This Post Helpful? 1
  • +
  • -

#5 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 19-November 18

Re: Reading DAT file

Posted 19 November 2018 - 07:49 PM

View Postmodi123_1, on 19 November 2018 - 06:58 PM, said:

You can split a string with 'split' around a comma.

Stream read with a while loop, string split into am array, add to a table.


Okay, thanks for your advice. Let me try.


Private Sub btnLoadDat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadDat.Click
        If IsSaveMode Then
            Exit Sub
        End If
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        If Me.txtDatFile.Text.Trim = "" Then
            Me.Cursor = System.Windows.Forms.Cursors.Default
            MessageBox.Show("Please, choose a file first")
            Exit Sub
        End If

        Me.oDtsDatReader.Tables("DatLine").Clear()
        Me.oDtsDatReder.Tables("DatH").Clear()

        Dim oFileInfo As System.IO.FileInfo
        Dim oStreamReader As System.IO.StreamReader
        Dim ErrorLogList As ArrayList
        Dim counterFileLine As Integer = 1

        Try

            oFileInfo = New System.IO.FileInfo(Me.txtDatReader.Text.Trim)

            oStreamReader = oFileInfo.OpenText

            Dim text As String
            Dim splittedText() As String
            Dim row As DataRow
            Dim txtID As String
            Dim splittedTxtID() As String
            Dim isHeaderSet As Boolean = False
            Dim skipThisID As String = ""
            Dim isThisRowValid As Boolean = True

            text = oStreamReader.ReadLine

            Dim currentID As String
            Dim currentSeries As Integer
            ErrorLogList = New ArrayList(0)

            While Not text Is Nothing
                If text.Trim <> "" Then
                    splittedText = text.Split(",")

...




the above code was my vb app. to read 2 files consists of header data and line data, meanwhile in this new code, I just want to read one file dat only.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14581
  • View blog
  • Posts: 58,445
  • Joined: 12-June 08

Re: Reading DAT file

Posted 19 November 2018 - 09:06 PM

I do not get what you wrote versus your code.

If your code is reading two files then.. make it not do that? I am not understanding.
Was This Post Helpful? 1
  • +
  • -

#7 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 19-November 18

Re: Reading DAT file

Posted 19 November 2018 - 10:06 PM

View Postmodi123_1, on 19 November 2018 - 09:06 PM, said:

I do not get what you wrote versus your code.

If your code is reading two files then.. make it not do that? I am not understanding.


Sorry I made your confusing. What I have written in my previous reply was about my current code. You are right that my code is reading two files but the files do not have column title.

what if the files have column titles ? I am wondering if you know of any good tutorial to read and formatting it. Attachment is my dat files. it consists of column titles and values.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14581
  • View blog
  • Posts: 58,445
  • Joined: 12-June 08

Re: Reading DAT file

Posted 19 November 2018 - 10:09 PM

It is a logic problem. Stream read in thr first line, use string split, create table columns with the resulting array. Each subsequent read would be in a loop for the data.
Was This Post Helpful? 1
  • +
  • -

#9 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 19-November 18

Re: Reading DAT file

Posted 28 November 2018 - 06:56 PM

View Postmodi123_1, on 19 November 2018 - 10:09 PM, said:

It is a logic problem. Stream read in thr first line, use string split, create table columns with the resulting array. Each subsequent read would be in a loop for the data.



I have tried to do that but the result is wrong.
I have attached the picture of the result for you review.

The code I used as follows:

Private Sub btnLoadGoodsReceipt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadGoodsReceipt.Click
        If IsSaveMode Then
            Exit Sub
        End If
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        If Me.txtGoodsReceiptFile.Text.Trim = "" Then
            Me.Cursor = System.Windows.Forms.Cursors.Default
            MessageBox.Show("Please, choose a file first")
            Exit Sub
        End If

        Me.oDtsGoodReceipt.Tables("GoodsReceipt").Clear()
  

        Dim oFileInfo As System.IO.FileInfo
        Dim oStreamReader As System.IO.StreamReader
        Dim ErrorLogList As ArrayList
        Dim counterFileLine As Integer = 1

        Try

            oFileInfo = New System.IO.FileInfo(Me.txtGoodsReceiptFile.Text.Trim)

            oStreamReader = oFileInfo.OpenText

            Dim text As String
            Dim splittedText() As String
            Dim row As DataRow
            Dim txtTgl As String
            Dim splittedTxtTgl() As String
            Dim isHeaderSet As Boolean = False
            Dim skipThisID As String = ""
            Dim isThisRowValid As Boolean = True

            text = oStreamReader.ReadLine

            Dim currentID As String
            Dim currentSeries As Integer
            ErrorLogList = New ArrayList(0)

            While Not text Is Nothing
                If text.Trim <> "" Then
                    splittedText = text.Split(",")
                    Dim _id As String = splittedText(0).Trim()
                    Dim ItemCode As String = splittedText(1).Trim()
                    Dim whse As String = splittedText(2).Trim
                End If
                If splittedText(0) = "_id" Then
                    Dim oItem As SAPbobsCOM.Items
                    oItem = SBO_Active_Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems)
                    oItem.GetByKey(splittedText(2))
                    row = Me.oDtsPenjualanProfitCenter.Tables("GoodsReceipt").NewRow
                    row("_id") = splittedText(0)
                    row("whse") = splittedText(2)
                    Me.oDtsGoodReceipt.Tables("GoodsReceipt").Rows.Add(row)
                
                text = oStreamReader.ReadLine.ToString()
                counterFileLine = counterFileLine + 1
            End While
            oStreamReader.Close()
            oStreamReader = Nothing
            oFileInfo = Nothing
            If ErrorLogList.Count > 0 Then
                Me.btnSave.Enabled = False
                Dim fileName As String = Logging.WriteToText(ErrorLogList, "VALIDATION ERROR")
                Dim oFrmLogViewer As frmLogViewer
                oFrmLogViewer = New frmLogViewer
                oFrmLogViewer.AutoLoad = True
                If fileName = "" Then
                    oFrmLogViewer.LoadFromFile = False
                    oFrmLogViewer.TextList = ErrorLogList
                Else
                    oFrmLogViewer.LoadFromFile = True
                    oFrmLogViewer.LogFileName = fileName
                End If
                oFrmLogViewer.ShowDialog()
            Else
                Me.btnSave.Enabled = True
            End If
        Catch ex As Exception
          
            MsgBox(ex.Message)
          
        End Try
        Me.Cursor = System.Windows.Forms.Cursors.Default
    End Sub




The data source is a dat file or csv file. I also have attached it. the file name is data source.


The number of rows of the CSV file changes at regular intervals.
I'm still unable to take these column or row values into a table in VB.NET. Can I please have some help on that? Thank you.

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#10 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 155
  • View blog
  • Posts: 972
  • Joined: 05-December 13

Re: Reading DAT file

Posted 28 November 2018 - 07:30 PM

First tidy up your code.

Second, apply using blocks to your stream reader. This will ensure all resources are released and disposed of properly.

Thirdly, get that while loop out of there. I think you'd be better putting it like, while reader.read and go from there. If you're still having problems after your spring cleaning, I will look at what you changed tomorrow. When your while loop cycles, what is happening? Start debugging

Also, you'd be better with a for each line received loop, then split. Perhaps feed your stream to a dictionary, and do a for each loop on the dictionary to split at ",".
Was This Post Helpful? 1
  • +
  • -

#11 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 19-November 18

Re: Reading DAT file

Posted 28 November 2018 - 08:38 PM

View PostSheepings, on 28 November 2018 - 07:30 PM, said:

First tidy up your code.

Second, apply using blocks to your stream reader. This will ensure all resources are released and disposed of properly.

Thirdly, get that while loop out of there. I think you'd be better putting it like, while reader.read and go from there. If you're still having problems after your spring cleaning, I will look at what you changed tomorrow. When your while loop cycles, what is happening? Start debugging

Also, you'd be better with a for each line received loop, then split. Perhaps feed your stream to a dictionary, and do a for each loop on the dictionary to split at ",".


I have apply the using blocks but still unable to display the row data. :helpsmilie:
I'll keep on struggling to solve it but if someone have an experience similar with me and can share it, I really appreciate it
Was This Post Helpful? 0
  • +
  • -

#12 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 19-November 18

Re: Reading DAT file

Posted 28 November 2018 - 10:06 PM

Dear All,


Finally my problem is solved. Thank you guys for all of your times and attentions to help me. have a nice day for your all wherever you are.

Here is my final code:

Private Sub btnLoadGoodsReceipt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadGoodsReceipt.Click
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        If Me.txtGoodsReceiptFile.Text.Trim = "" Then
            Me.Cursor = System.Windows.Forms.Cursors.Default
            MessageBox.Show("Please, choose a file first")
            Exit Sub
        End If
        Me.oDtsPenjualanProfitCenter.Tables("GoodsReceipt").Clear()
        Dim oFileInfo As System.IO.FileInfo
        Dim oStreamReader As System.IO.StreamReader
        Dim ErrorLogList As ArrayList
        Dim counterFileLine As Integer = 1

        Try

            oFileInfo = New System.IO.FileInfo(Me.txtGoodsReceiptFile.Text.Trim)
            oStreamReader = oFileInfo.OpenText
            Dim text As String
            Dim splittedText() As String
            Dim row As DataRow
            text = oStreamReader.ReadLine()
            ErrorLogList = New ArrayList(0)

            While Not text Is Nothing
                If text.Trim <> "" Then
                    splittedText = text.Split(",")
                    If splittedText(0) <> "_id" Then
                        row = Me.oDtsGoodReceiptTrans.Tables("GoodsReceipt").NewRow
                        row("_id") = splittedText(0)
                        row("ItemCode") = splittedText(1)
                        row("whse") = splittedText(2)
                        Me.oDtsGoodReceiptTrans.Tables("GoodsReceipt").Rows.Add(row)
                    End If
                    text = oStreamReader.ReadLine
                    counterFileLine = counterFileLine + 1
                End If
            End While
            oStreamReader.Close()
            oStreamReader = Nothing
            oFileInfo = Nothing
            If ErrorLogList.Count > 0 Then
                Me.btnSave.Enabled = False
                Dim fileName As String = Logging.WriteToText(ErrorLogList, "VALIDATION ERROR")
                Dim oFrmLogViewer As frmLogViewer
                oFrmLogViewer = New frmLogViewer
                oFrmLogViewer.AutoLoad = True
                If fileName = "" Then
                    oFrmLogViewer.LoadFromFile = False
                    oFrmLogViewer.TextList = ErrorLogList
                Else
                    oFrmLogViewer.LoadFromFile = True
                    oFrmLogViewer.LogFileName = fileName
                End If
                oFrmLogViewer.ShowDialog()
            Else
                Me.btnSave.Enabled = True
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Me.Cursor = System.Windows.Forms.Cursors.Default
    End Sub



the result I've attached in this reply. The problem is now solved. Thanks God of Israel.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#13 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 155
  • View blog
  • Posts: 972
  • Joined: 05-December 13

Re: Reading DAT file

Posted 29 November 2018 - 05:36 AM

Great you got it solved, but I don't see where you have applied using blocks on the reader. You'd do it like so ::

        Dim line As String
        Using reader As StreamReader = New StreamReader("file.txt")'Path to file
            line = reader.ReadLine
        End Using

I would advise familiarising yourself with using blocks where ever you can, as you can use them in a range of situations @Steve7419, and because they manage their own resources and dispose of them when they are done using, it will make your application less resource heavy.
Was This Post Helpful? 1
  • +
  • -

#14 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 19-November 18

Re: Reading DAT file

Posted 29 November 2018 - 07:00 PM

View PostSheepings, on 29 November 2018 - 05:36 AM, said:

Great you got it solved, but I don't see where you have applied using blocks on the reader. You'd do it like so ::

        Dim line As String
        Using reader As StreamReader = New StreamReader("file.txt")'Path to file
            line = reader.ReadLine
        End Using

I would advise familiarising yourself with using blocks where ever you can, as you can use them in a range of situations @Steve7419, and because they manage their own resources and dispose of them when they are done using, it will make your application less resource heavy.


Yes, I did use it. But I don't use Using ... end using. I use "text" in my code instead of "line". Thanks for your reply.
Was This Post Helpful? 0
  • +
  • -

#15 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 155
  • View blog
  • Posts: 972
  • Joined: 05-December 13

Re: Reading DAT file

Posted 29 November 2018 - 08:30 PM

What I posted was an example of how to use it according to Microsoft website on MSDN. Once again, if you don't know what the hell it does, would you not read up on it before commenting? I never said it would work as is. What I said was you need to implement using blocks, which you haven't.

There is no point coming to a support forum, posting your code, asking for help, and then ignoring what was recommended to you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1