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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AP Invoice Insert Issue (C#, ACCPAC 5.3)

Status
Not open for further replies.

SandmanMS

Programmer
Feb 24, 2006
5
US
Hi guys,

I just got thrown into working with our importer program and I'm having an issue with the AP Invoice importation. Every once in a while when I call the Insert() method it will throw a SystemAccessViolation and say that protected memory was trying to be accessed. Any ideas? Here's the code. I'm using C# and ACCPAC 5.3a

ACCPACXAPILib.xapiView APINVCPOST = (ACCPACXAPILib.xapiView) m_session.OpenView("AP0039", "AP");
ACCPACXAPILib.xapiFields APINVCPOSTFields = APINVCPOST.Fields;

ACCPACXAPILib.xapiView APINVOICEbatch = (ACCPACXAPILib.xapiView) m_session.OpenView("AP0020", "AP");
ACCPACXAPILib.xapiFields APINVOICEbatchFields = APINVOICEbatch.Fields;

ACCPACXAPILib.xapiView APINVOICEheader = (ACCPACXAPILib.xapiView) m_session.OpenView("AP0021", "AP");
ACCPACXAPILib.xapiFields APINVOICEheaderFields = APINVOICEheader.Fields;

ACCPACXAPILib.xapiView APINVOICEdetail1 = (ACCPACXAPILib.xapiView) m_session.OpenView("AP0022", "AP");
ACCPACXAPILib.xapiFields APINVOICEdetail1Fields = APINVOICEdetail1.Fields;

ACCPACXAPILib.xapiView APINVOICEdetail2 = (ACCPACXAPILib.xapiView) m_session.OpenView("AP0023", "AP");
ACCPACXAPILib.xapiFields APINVOICEdetail2Fields = APINVOICEdetail2.Fields;

APINVOICEbatch.Compose(new Object[] {APINVOICEheader});
APINVOICEheader.Compose(new Object[] {APINVOICEbatch, APINVOICEdetail1, APINVOICEdetail2});
APINVOICEdetail1.Compose(new Object[] {APINVOICEheader, APINVOICEbatch});
APINVOICEdetail2.Compose(new Object[] {APINVOICEheader});

int Counter = 1;

APINVOICEbatch.Init();
APINVOICEheader.Init();

APINVOICEbatchFields.Item("BTCHDESC").PutWithoutVerification ("Photographer invoice batch"); //Description
APINVOICEbatchFields.Item("INVCTYPE").Value = 1;
APINVOICEbatchFields.Item("BTCHTYPE").Value = 5;
APINVOICEbatch.Update();
APINVOICEheaderFields.Item("CNTITEM").PutWithoutVerification (Counter); //Entry Number
APINVOICEdetail1.Cancel();
APINVOICEdetail2.Cancel();
APINVOICEheader.Init();

for (int i=0; i<dtHeader.Rows.Count; i++)
{
string idinvc = Convert.ToString(dtHeader.Rows["idinvc"]);
decimal AMTINVCTOT = Convert.ToDecimal(dtHeader.Rows["AMTINVCTOT"]);
try
{
Log(" Processing IDINVC " + idinvc + ", ORDRNBR = " + dtHeader.Rows["ORDRNBR"].ToString());
if (AMTINVCTOT != 0)
{
APINVOICEheader.RecordClear();
APINVOICEheader.RecordGenerate(false);

APINVOICEheaderFields.Item("IDVEND").Value = dtHeader.Rows["idvend"];
APINVOICEheaderFields.Item("TEXTTRX").Value = dtHeader.Rows["TEXTTRX"];

if (dtHeader.Rows["INVCAPPLTO"].ToString() != "")
{

APINVOICEheaderFields.Item("INVCAPPLTO").Value = dtHeader.Rows["INVCAPPLTO"];
}
DateTime invdate = Convert.ToDateTime(dtHeader.Rows["DATEINVC_CONVERTED"]);


APINVOICEheaderFields.Item("IDINVC").Value = dtHeader.Rows["idinvc"];

APINVOICEheaderFields.Item("DATEINVC").Value = invdate;

APINVOICEheader.Process();

APINVOICEheaderFields.Item("AMTGROSTOT").Value = Math.Abs(AMTINVCTOT); //Ship-To Location Code
APINVOICEheaderFields.Item("ORDRNBR").Value = dtHeader.Rows["ORDRNBR"];

DataRow[] rows = dtDetails.Select("IDINVC = '" + idinvc + "'");

for (int j = 0; j < rows.Length; j++)
{
Log(" Processing UNIQUEAPINVOICEDETAIL " + Convert.ToInt32(rows[j]["UNIQUEAPINVOICEDETAIL"]));

APINVOICEdetail1.RecordClear();
APINVOICEdetail1.RecordGenerate(false);
APINVOICEdetail1.Fields.Item("IDGLACCT").Value = rows[j]["IDGLACCT"];
APINVOICEdetail1.Fields.Item("COMMENT").Value = rows[j]["TEXTLINE"];
APINVOICEdetail1.Fields.Item("IDITEM").Value = rows[j]["UNIQUEAPINVOICEDETAIL"];
APINVOICEdetail1.Fields.Item("BILLDATE").Value = rows[j]["DATEINVC"];
APINVOICEdetail1.Fields.Item("SWIBT").Value = rows[j]["SWIBT"];
//APINVOICEdetail1.Fields.Item("AMTGLDIST").Value = rows[j]["AMTGLDIST"];
APINVOICEdetail1.Fields.Item("AMTDIST").Value = rows[j]["AMTGLDIST"];
APINVOICEdetail1.Insert();

sql = "UPDATE TBL_ACC_AP_INVOICEDETAILS SET NewImport = 0, APINVOICEDETAILSIMPORTDATE = GETDATE(), ImporttoACCPACDate = GETDATE() WHERE UNIQUEAPINVOICEDETAIL = " + Convert.ToInt32(rows[j]["UNIQUEAPINVOICEDETAIL"]);
Log("SQL: " + sql);
da.SelectCommand.CommandText = sql;
da.SelectCommand.ExecuteNonQuery();

}


APINVOICEheader.Insert();


sql = "UPDATE TBL_ACC_AP_INVOICEHEADER SET NewImport = 0, ImporttoACCPACDate = GETDATE() WHERE IDINVC = '" + idinvc + "'";
Log("SQL: " + sql);
da.SelectCommand.CommandText = sql;
da.SelectCommand.ExecuteNonQuery();

Counter = Counter + 1;
Log("Succeeded Processing IDINVC " + idinvc);
APINVOICEheader.Cancel();
APINVOICEheader.Cancel();
}
}
catch (Exception ex)
{
if (errorcount == 0) summary += CRLF + CRLF + " Errors:";
errorcount++;
summary += CRLF + " SELECT * FROM [colacc]..TBL_ACC_AP_INVOICEHEADER WHERE IDINVC = '" + idinvc + "'";

Log(" Exception processing IDINVC " + idinvc + ": " + ex);

ACCPACXAPILib.xapiErrors aError = m_session.Errors;
foreach (ACCPACXAPILib.xapiError x1 in aError)
{
string curError = "IDINVC: " + idinvc + " ACCPAC Error Priority: " + x1.Priority + " Description: " + x1.Description;
summary = summary + curError + CRLF;
Log(curError);
}
m_session.ClearErrors();

sql = "UPDATE TBL_ACC_AP_INVOICEHEADER SET NewImport = 0, ImporttoACCPACDate = GETDATE(), ErrorOccurred = 1 WHERE IDINVC = '" + idinvc + "'";
Log("SQL: " + sql);
da.SelectCommand.CommandText = sql;
da.SelectCommand.ExecuteNonQuery();

APINVOICEdetail1.Cancel();
APINVOICEheader.Cancel();

}
}
 
Once in a while"... That means it works sometimes. What are the circumstances when it doesn't?

Jay Converse
IT Director
Systemlink, Inc.
 
Well it will import a few of the invoices and then fail with that exception. The insert on the details runs without any errors but then when the insert runs on the header it throws that exception. The data looks like it is fine.
 
I'm still not clear. Do you get one or two invoices in the batch, and it crashes on the third, or do you not get any invoices?

Jay Converse
IT Director
Systemlink, Inc.
 
That's the thing, it's not a set number of invoices that passes before it crashes. Sometimes 5 will go in, sometimes 20, sometimes 0 but it almost always eventually hits that error and crashes. The batch itself never posts because it crashes before it reachs that part of the code, but the inserts occur into the batch.
 
Well, the batch never posts because you don't have any posting code in there.

Being a VB/VBA guy, I would use the Accpac RVSPY and DBSPY utilities to see if there are any database exceptions. I don't know if DBSPY runs with a C# program.

Since it works sometimes, this sounds like an environmental problem, like a database timeout.

Jay Converse
IT Director
Systemlink, Inc.
 
Actually I just forgot to go far down enough to include the posting code. I'll check those two tools out and see if I can find anything.
 
Here is the exception:

Exception processing IDINVC 194924: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at ACCPACXAPILib.xapiFieldClass.set_Value(Object pVal)
at ACCPAC_Imports.ACCPAC_Main.AddAPInvoice()

It then tries to cancel the invoice which throws another System.AccessViolationException and then crashes.
 
Is it possible you've got dirty data? What if you duplicated the 5 invoices that successfully inserted about 10 times and see if you can get 50 entries?

Jay Converse
IT Director
Systemlink, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top