11 Replies - 700 Views - Last Post: 06 December 2018 - 06:59 AM Rate Topic: -----

#1 Steve7419   User is offline

  • New D.I.C Head

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

Only one row that can be saved

Posted 03 December 2018 - 03:51 AM

Dear All,


I have one dat file sent from barcode scanner using FTP server and its contents more than one lines, e.g. 6 lines. All of lines must be saved into one document of an ERP application. The document itself contain two tables, i.e. OIGN as header table and IGN1 as line tables

Here is the code that I use to save the data:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        IsSaveMode = True
        Dim ErrorLogList As ArrayList
        ErrorLogList = New ArrayList(0)
        Dim addStatus As Integer

        Try
            SBO_Active_Company.StartTransaction()
            Dim oGoodReceipt As SAPbobsCOM.Documents
            Dim _id As String
            Dim oRows() As DataRow
            Dim headerCounter As Integer
            Dim rowCounter As Integer
            Dim oGoodsReceiptTable As DataTable
            oGoodsReceiptTable = Me.oDtsGoodReceiptsCenter.Tables("GoodsReceipt")
            _id = CType(Me.dtgGoodsReceipt.Item(rowCounter, 0), String)
            For headerCounter = 0 To oGoodsReceiptTable.Rows.Count - 1
                oGoodReceipt = SBO_Active_Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oInventoryGenEntry)
                oGoodReceipt.Series = "599"
                oGoodReceipt.DocDate = Date.Now.ToString("yyyy-MM-dd")
                oGoodReceipt.Reference2 = "addon"
                oGoodReceipt.Comments = "Imp.GR."
                oGoodReceipt.JournalMemo = "Imp.GR."
                



                oRows = oGoodsReceiptTable.Select("[_id] = " & _id)
                For rowCounter = 0 To oRows.Length - 1
                    'oGoodReceipt.Lines.visorder = CType(oRows(rowCounter).Item("_id"), String)
                    oGoodReceipt.Lines.ItemCode = CType(oRows(rowCounter).Item("ItemCode"), String)
                    oGoodReceipt.Lines.AccountCode = "5113-0002"
                    oGoodReceipt.Lines.Quantity = CType(oRows(rowCounter).Item("Qty"), Double)
                    oGoodReceipt.Lines.WarehouseCode = CType(oRows(rowCounter).Item("whse"), String)
                    oGoodReceipt.Lines.Add()
                    'If rowCounter < oRows.Length - 1 Then
                    '    oGoodReceipt.Lines.Add()

                    'End If
                Next
                ' Dim addStatus As Integer
                Dim sErrMsg As String
                sErrMsg = ""
                addStatus = oGoodReceipt.Add
                If addStatus <> 0 Then
                    SBO_Active_Company.GetLastError(addStatus, sErrMsg)
                    ErrorLogList.Add(_id & " failed." & Chr(13) & Chr(13) & addStatus & " - " & sErrMsg)
                    If SBO_Active_Company.InTransaction Then
                        SBO_Active_Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack)
                    End If
                    Exit Sub
                End If
            Next
            If SBO_Active_Company.InTransaction Then
                SBO_Active_Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit)
            End If
            If ErrorLogList.Count > 0 Then
                Dim fileName As String = Logging.WriteToText(ErrorLogList, "SAVE 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 = False
                MessageBox.Show("Goods Receipt Succeed")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            If SBO_Active_Company.InTransaction Then
                SBO_Active_Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack)
            End If
        End Try
        ' IsSaveMode = False
        Me.Cursor = System.Windows.Forms.Cursors.Default
    End Sub




The result is wrong. The above code can only create the first line repeatedly until 6 times. The other lines are not created.

I have attached the result and the data source. The file name of result is good receipt result.jpg. The data source is data_source.jpg.
Any help would be great and appreciated.
thanks

Regards,
Steve

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Only one row that can be saved

#2 Sheepings   User is offline

  • Senior Programmer
  • member icon

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

Re: Only one row that can be saved

Posted 03 December 2018 - 06:46 AM

The result is wrong; how?

Why do you have nested for loops?

This is not doing what you think its doing ::
For rowCounter = 0 To oRows.Length - 1

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: Only one row that can be saved

Posted 03 December 2018 - 07:36 AM

View PostSheepings, on 03 December 2018 - 06:46 AM, said:

The result is wrong; how?

Why do you have nested for loops?

This is not doing what you think its doing ::
For rowCounter = 0 To oRows.Length - 1


Thanks for your solution. I thought it was. i still have no idea how loop works.

I've tried to use this following :

For headerCounter = 0 To oGoodsReceiptTable.Rows.Count - 1


but it is getting error.
Was This Post Helpful? 0
  • +
  • -

#4 Sheepings   User is offline

  • Senior Programmer
  • member icon

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

Re: Only one row that can be saved

Posted 03 December 2018 - 11:15 AM

Well the documentation is there to serve you. It up to you to read and learn it... For what you need to know about most loops are mentioned in this document.

Read through the related links in the article for each loop type.
Was This Post Helpful? 1
  • +
  • -

#5 Sheepings   User is offline

  • Senior Programmer
  • member icon

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

Re: Only one row that can be saved

Posted 03 December 2018 - 01:33 PM

Steve, this is not correct and is not doing what you think it is either. Learn both concatenationand substring
29            oRows = oGoodsReceiptTable.Select("[_id] = " & _id)

And this
17            _id = CType(Me.dtgGoodsReceipt.Item(rowCounter, 0), String)

does not need to be cast as a string if you would change the dimensions of the declaration of _id to integer. Where did you get the code from? What is the value of rowCounter when it executes? What is the column index of the item you are trying to select, and are there rows for the index you are assigning?

If you are copying and pasting code from the internet without knowing what it does, you are going to have a lot of problems. Would it not be best to grab a book on the subject language from the library or spend a few hours at MSDN researching the basics of what you're doing.
Was This Post Helpful? 1
  • +
  • -

#6 Steve7419   User is offline

  • New D.I.C Head

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

Re: Only one row that can be saved

Posted 03 December 2018 - 05:02 PM

View PostSheepings, on 03 December 2018 - 01:33 PM, said:

Steve, this is not correct and is not doing what you think it is either. Learn both concatenationand substring
29            oRows = oGoodsReceiptTable.Select("[_id] = " & _id)

And this
17            _id = CType(Me.dtgGoodsReceipt.Item(rowCounter, 0), String)

does not need to be cast as a string if you would change the dimensions of the declaration of _id to integer. Where did you get the code from? What is the value of rowCounter when it executes? What is the column index of the item you are trying to select, and are there rows for the index you are assigning?

If you are copying and pasting code from the internet without knowing what it does, you are going to have a lot of problems. Would it not be best to grab a book on the subject language from the library or spend a few hours at MSDN researching the basics of what you're doing.


Sheepings,
I did not copy from the internet. I know what it does but sometimes I probably missed something in my code.

this code,
17            _id = CType(Me.dtgGoodsReceipt.Item(rowCounter, 0), String)
,
is representing the _id column, i.e. 1,2,3,4,5 and 6.
I think it works well because I have tried using a new code. You can see the result2.jpg.
You can see the _id column values in the data_source.jpg picture.

And this one,
29            oRows = oGoodsReceiptTable.Select("[_id] = " & _id)
,
will be used to read the _id. So, rowcounter will be used in the For Statement as follows:
For rowCounter = 0 To oGoodsReceiptTable.Rows.Count - 1


In the result2.jpg, the itemcode is still using the 1st line. Any help would be great.

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 Sheepings   User is offline

  • Senior Programmer
  • member icon

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

Re: Only one row that can be saved

Posted 03 December 2018 - 07:02 PM

I thought you missed my point about not needing to convert string to integer, but I was mistaken.

I was wrong by assuming you should be passing a Integer, when select on the datatable actually takes a string representation. Thanks for pointing that out, and sorry if I caused confusion. I've been running on little to no sleep the last 3 days.

Plus if I'm honest, datasets and datagridviews are to dated for me and I honestly tend to stay away from them, my info is limited on them, and normally don't give advice on them. Any databinding I need doing would be done in WPF :turned: After looking up Datatable.select to learn more on it myself, I see it takes a string. But your passing [_id] = " & _id as your string.

But there will not be an ID with this value "[_id] = " & _id, perhaps _id? That string as you have it; will output like this: [_id] = 0 etc.

And the reason you are only getting one row, is because
17            _id = CType(Me.dtgGoodsReceipt.Item(rowCounter, 0), String)
the zero is not increasing from 0 to the last row as its iterating.

Your code is confusing, because you are using rowCounter in a field that's for specifying column index, and then the row index is set to 0.

Attached Image

I likely won't be online tomorrow, so maybe someone else will chime in to help you fix the rest up.

This post has been edited by Sheepings: 03 December 2018 - 07:08 PM

Was This Post Helpful? 0
  • +
  • -

#8 Steve7419   User is offline

  • New D.I.C Head

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

Re: Only one row that can be saved

Posted 04 December 2018 - 05:56 AM

It is okay. my you have a nice day.

I am really very sorry that my code have made you confusing. I have tried hard to fix it until now but not successful.

Anyone can help would be really appreciated. Thank you.

This post has been edited by andrewsw: 04 December 2018 - 06:24 AM
Reason for edit:: removed previous quote, just use the Reply button

Was This Post Helpful? 0
  • +
  • -

#9 Steve7419   User is offline

  • New D.I.C Head

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

Re: Only one row that can be saved

Posted 05 December 2018 - 09:41 AM

Dear Sheepings and all,


I managed to solve the problem. All rows of my dat file can be saved automatically now.
The reason why my application can't insert all rows because I forgot to include the header table of good receipt document in my code. The header table is required the ERP application.

The data for good receipt header table is different with good receipt lines table but both always have the same docentry or entry running number.

So, the headercounter will be used as line id counting or line number counting of all the data in the dat file.

Here is my final code:
Try
            Dim headerCounter As Integer
            Dim oGoodsReceiptTableH As DataTable
            oGoodsReceiptTableH = Me.oGoodReceiptTrans.Tables("GoodsReceipt")
            SBO_Active_Company.StartTransaction()
            For headerCounter = 0 To oGoodsReceiptTableH.Rows.Count - 1
                Dim oGoodReceipt As SAPbobsCOM.Documents
                Dim _id As String
                _id = CType(Me.dtgGoodsReceipt.Item(headerCounter, 0), String)
                oGoodReceipt = SBO_Active_Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oInventoryGenEntry)
                oGoodReceipt.Series = "599"
                oGoodReceipt.DocDate = Date.Now.ToString("yyyy-MM-dd")
                oGoodReceipt.Reference2 = "addon"
                oGoodReceipt.Comments = "Imp.GR."
                oGoodReceipt.JournalMemo = "Imp.GR."

                Dim rowCounter As Integer
                Dim oGoodsReceiptTable As DataTable
                oGoodsReceiptTable = Me.oGoodReceiptTrans.Tables("GoodsReceipt")
                Dim oRows() As DataRow
                oRows = oGoodsReceiptTable.Select("[_id] = " & _id)

                For rowCounter = 0 To oRows.Length - 1

                    oGoodReceipt.Lines.ItemCode = CType(oRows(rowCounter).Item("ItemCode"), String)
                    oGoodReceipt.Lines.AccountCode = "5113-0002"
                    oGoodReceipt.Lines.Quantity = CType(oRows(rowCounter).Item("Qty"), Double)
                    oGoodReceipt.Lines.WarehouseCode = CType(oRows(rowCounter).Item("whse"), String)

                    If rowCounter < oRows.Length - 1 Then
                        oGoodReceipt.Lines.Add()

                    End If
                Next
                Dim addStatus As Integer
                Dim sErrMsg As String
                sErrMsg = ""
                addStatus = oGoodReceipt.Add
                If addStatus <> 0 Then
                    SBO_Active_Company.GetLastError(addStatus, sErrMsg)
                    ErrorLogList.Add(_id & " failed." & Chr(13) & Chr(13) & addStatus & " - " & sErrMsg)
                    If SBO_Active_Company.InTransaction Then
                        SBO_Active_Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack)
                    End If
                    Exit Sub
                End If
            Next



Thanks for your attention and helps.

Regards,
Steve
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: Only one row that can be saved

Posted 05 December 2018 - 10:18 AM

Good to see you got it working Steve.

Datasets and DGV are my weak points in programming, maybe because I've never really cared a great deal for the old 90's way of doing things.

If you or anyone else can explain how this works ("[_id] = " & _id), I'd love to know. :unsure:
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: Only one row that can be saved

Posted 05 December 2018 - 10:55 PM

View PostSheepings, on 05 December 2018 - 10:18 AM, said:

Good to see you got it working Steve.

Datasets and DGV are my weak points in programming, maybe because I've never really cared a great deal for the old 90's way of doing things.

If you or anyone else can explain how this works ("[_id] = " & _id), I'd love to know. :unsure:/>


Yes, sure and thanks.

Anyway, I am sure that some of old ways is still useful in this millennial era although it has limitation.

for this ("[_id] = " & _id), it uses bracket [] and it is similar with SQL query or syntax.
the _id itself is a column code that its value will be used as running number or sequence number of data that origins from dat file.

09
	                _id = CType(Me.dtgGoodsReceipt.Item(headerCounter, 0), String)


My application really needs the _id. The above code will count the _id number or row number sequence i.e. range from 1 to 6 or other.

Regards,
Steve
Was This Post Helpful? 1
  • +
  • -

#12 Sheepings   User is offline

  • Senior Programmer
  • member icon

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

Re: Only one row that can be saved

Posted 06 December 2018 - 06:59 AM

Thanks, I didn't see it as an SQL query, and so it didn't make sense to me. And thanks to one of the staff here who explained it to me as well. +1 for sharing your solution. :)

Btw, if you are using Version 9 of the DI API, there is a memory leak which has been reported. Just thought I'd give you a heads-up, if you're using that version.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1