Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ACCPACXAPILib Error: Attempted to read or write protected memory 1

Status
Not open for further replies.

Ho3n3r

Programmer
Sep 26, 2012
23
ZA
Hi Guys

I am having an issue which for the life of me I cannot recreate on my testing environment, and my client only gets it once in a while. For example, she has captured 30 new members this week, and she only got it once after doing it for those 30 times.

It's an application I am maintaining and editing, so if anything looks wrong in the code, please feel free to tell me.

Here's the code block that the exception is thrown in:

Code:
    Public Sub addMemberOptionalField(ByVal sMemNo As String, _
                                            ByVal sField As String, _
                                            ByVal sValue As String, _
                                            ByVal AccPacPassword As String, _
                                            ByVal bUpdate As Boolean)
        Dim exSession As ACCPACXAPILib.xapiSession
        exSession = New ACCPACXAPILib.xapiSession
        Dim reader As New AppSettingsReader
        Dim dbname As String = reader.GetValue("DBName", GetType(String)).ToString()
        Try
            exSession.Open("ADMIN", AccPacPassword, dbname, Now, 0)
            Dim temp As Boolean
            Dim ARCUSTOMER1header As ACCPACXAPILib.xapiView
            Dim ARCUSTOMER1headerFields As ACCPACXAPILib.xapiFields
            ARCUSTOMER1header = exSession.OpenView("AR0024", "AR")
            ARCUSTOMER1headerFields = ARCUSTOMER1header.Fields
            Dim ARCUSTOMER1detail As ACCPACXAPILib.xapiView
            Dim ARCUSTOMER1detailFields As ACCPACXAPILib.xapiFields
            ARCUSTOMER1detail = exSession.OpenView("AR0400", "AR")
            ARCUSTOMER1detailFields = ARCUSTOMER1detail.Fields
            ARCUSTOMER1header.Compose(ARCUSTOMER1detail)
            ARCUSTOMER1detail.Compose(ARCUSTOMER1header)
            temp = ARCUSTOMER1header.Exists
            ARCUSTOMER1header.Init()
            ARCUSTOMER1headerFields.Item("IDCUST").Value = sMemNo                   ' Customer Number
            ARCUSTOMER1header.Read()
            If Not bUpdate Then
                temp = ARCUSTOMER1detail.Exists
                ARCUSTOMER1detail.RecordClear()
                temp = ARCUSTOMER1detail.Exists
                ARCUSTOMER1detail.RecordGenerate(False)
                ARCUSTOMER1detailFields.Item("IDCUST").Value = sMemNo
                ARCUSTOMER1detailFields.Item("OPTFIELD").PutWithoutVerification(sField)                   ' Optional Field
                ARCUSTOMER1detailFields.Item("VALIFTEXT").Value = sValue      ' Text Value
                ARCUSTOMER1detail.Insert()
            Else
                ARCUSTOMER1detailFields.Item("IDCUST").Value = sMemNo
                ARCUSTOMER1detailFields.Item("OPTFIELD").PutWithoutVerification(sField)                   ' Optional Field
                ARCUSTOMER1detail.Read()
                ARCUSTOMER1detailFields.Item("VALIFTEXT").Value = sValue      ' Text Value
                ARCUSTOMER1detail.Update()
            End If
            ARCUSTOMER1detail.Read()
            ARCUSTOMER1header.Update()
        Catch ex As Exception
            MsgBox(ex.Message & " " & GetAccpacError(exSession), MsgBoxStyle.Critical, "Save Optional Fields")
        Finally
            exSession.Close()
            exSession = Nothing
        End Try
    End Sub

Any advice here would be very much appreciated.
 
Oh and the complete error reads like the following:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Thanks in advance.
 
It could be attempting to write to a read only field.
The code does not test if a customer exists, it assumes the customer exists. If a customer does not exist then you are going to run into errors.
 
This is what I would do:
Code:
Public Sub addMemberOptionalField(ByVal sMemNo As String, _
                                            ByVal sField As String, _
                                            ByVal sValue As String, _
                                            ByVal AccPacPassword As String, _
                                            ByVal bUpdate As Boolean)
        Dim exSession As ACCPACXAPILib.xapiSession
        exSession = New ACCPACXAPILib.xapiSession
        Dim reader As New AppSettingsReader
        Dim dbname As String = reader.GetValue("DBName", GetType(String)).ToString()
        Try
            exSession.Open("ADMIN", AccPacPassword, dbname, Now, 0)

            Dim ARCUSTOMER1header As ACCPACXAPILib.xapiView
            Dim ARCUSTOMER1detail As ACCPACXAPILib.xapiView

            ARCUSTOMER1header = exSession.OpenView("AR0024", "AR")
            ARCUSTOMER1detail = exSession.OpenView("AR0400", "AR")

            ARCUSTOMER1header.Compose(ARCUSTOMER1detail)
            ARCUSTOMER1detail.Compose(ARCUSTOMER1header)

            ARCUSTOMER1header.Init()
            ARCUSTOMER1headerFields.Item("IDCUST").PutWithoutVerification sMemNo                   ' Customer Number
            If ARCUSTOMER1header.Read() Then
            'Customer exists
            If Not bUpdate Then
                ARCUSTOMER1detail.RecordClear()
                ARCUSTOMER1detail.RecordGenerate(False)
                ARCUSTOMER1detail.Fields.Item("IDCUST").Value = sMemNo
                ARCUSTOMER1detail.Fields.Item("OPTFIELD").PutWithoutVerification(sField)                   ' Optional Field
                ARCUSTOMER1detail.Fields.Item("VALIFTEXT").Value = sValue      ' Text Value
                ARCUSTOMER1detail.Insert()
            Else
                ARCUSTOMER1detail.Fields.Item("IDCUST").Value = sMemNo
                ARCUSTOMER1detail.Fields.Item("OPTFIELD").PutWithoutVerification(sField)                   ' Optional Field
                ARCUSTOMER1detail.Read()
                ARCUSTOMER1detail.Fields.Item("VALIFTEXT").Value = sValue      ' Text Value
                ARCUSTOMER1detail.Update()
            End If
            Else
                'Customer does not exist
            End If
            ARCUSTOMER1header.Update()
        Catch ex As Exception
            MsgBox(ex.Message & " " & GetAccpacError(exSession), MsgBoxStyle.Critical, "Save Optional Fields")
        Finally
            exSession.Close()
            exSession = Nothing
        End Try
    End Sub
 
Ettienne said:
It could be attempting to write to a read only field.
The code does not test if a customer exists, it assumes the customer exists. If a customer does not exist then you are going to run into errors.

Ettienne said:
This is what I would do:

// Your Code

Thanks a lot Ettienne for the advice.

Can this verification of existing client prevent the specific error I posted?

What is odd is that the client has mentioned that she has clicked OK on the error message box, and sometimes when clicking SAVE MEMBER, it goes through.

It is very difficult to reproduce, as I have never once encountered it on testing.

Thank you again. I cannot tell you in words how frustrating this error has become.
 
It may or may not help. I don't know what's causing the error but my guess is maybe a customer that does not exist. I always test that a records exists before writing data, it's just good programming practice.

Add code to write errors and data to a log file, then check the log for clues.
It could also be that the customer number has something like a quote that is causing a problem.

I just spotted a mistake on my part, move ARCUSTOMER1header.Update() up 3 lines to before the Else.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top