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();
}
}
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();
}
}