Ho
In oComany it shows null value. It gives error :Object Reference not set to an instance of the Object.
When i write code of PendingInvoice in Constructor it works fine
C#
public partial class Form1 : Form
{
public static SAPbouiCOM.Application oApplication = null;
public static SAPbobsCOM.Company oCompany = null;
public static SAPbouiCOM.Form oForm = null;
public Form1()
{
//InitializeComponent();
SAPbobsCOM.Company oCompany = new SAPbobsCOM.Company();
oCompany.Server = "[email protected]:30015";
oCompany.DbServerType = BoDataServerTypes.dst_HANADB;
oCompany.CompanyDB = "TestE";
oCompany.UserName = "manager";
oCompany.Password = "123";
oCompany.language = BoSuppLangs.ln_English;
oCompany.UseTrusted = true;
int connectionResult = oCompany.Connect();
if (connectionResult != 0)
{
throw new Exception("Error connecting to SAP Business One: " + oCompany.GetLastErrorDescription());
}
PendingInvoice();
}
private void PendingInvoice()
{
string _StrSQL = "SELECT 'Invoice',T0.\"TransId\", T0.\"DocNum\", T0.\"DocDate\", T0.\"BPLName\", T0.\"CardCode\" " +
", T0.\"CardName\", T0.\"DocTotal\", T4.\"U_UTL_IRN\" " +
"FROM \"Test\".OINV T0 " +
"INNER JOIN CRD1 T3 ON T0.\"CardCode\" = T3.\"CardCode\" " +
"INNER JOIN INV12 T5 ON T5.\"DocEntry\" = T0.\"DocEntry\" " +
"LEFT JOIN \"Test\".\"@AUTL_MDEXTH\" T4 ON T0.\"DocEntry\" = T4.\"U_UTL_BaseEntry\" " +
"where T0.\"CardCode\" = T3.\"CardCode\" and T0.\"PayToCode\" = T3.\"Address\" and T3.\"AdresType\" = 'B' and T3.\"GSTRegnNo\" <> '' " +
"and T0.\"DocEntry\" NOT IN(Select A4.\"U_UTL_BaseEntry\" from \"test\".\"@AUTL_MDEXTH\" A4 where A4.\"U_UTL_IST\" <> 'F' ) " +
"and T5.\"LocGSTN\" <> T5.\"BpGSTN\" and T0.\"BPLId\" = '28'" + "";
SAPbobsCOM.Recordset oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
oRecordSet.DoQuery(_StrSQL);
}
}Thanks

Prasad RaveendranRank #320 · 7.8K reputationPosted Mar 16, 2025, 1:49 AM EDT
public partial class Form1 : Form { public static SAPbouiCOM.Application oApplication = null; public static SAPbobsCOM.Company oCompany = null; public static SAPbouiCOM.Form oForm = null; public Form1() { // Initialize SAP Business One connection ConnectToSAP(); PendingInvoice(); } private void ConnectToSAP() { try { oCompany = new SAPbobsCOM.Company { Server = "[email protected]:30015", DbServerType = BoDataServerTypes.dst_HANADB, CompanyDB = "TestE", UserName = "manager", Password = "123", // Move this to a config file or environment variable language = BoSuppLangs.ln_English }; int connectionResult = oCompany.Connect(); if (connectionResult != 0) { throw new Exception("Error connecting to SAP Business One: " + oCompany.GetLastErrorDescription()); } } catch (Exception ex) { MessageBox.Show("SAP Connection Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void PendingInvoice() { if (oCompany == null || !oCompany.Connected) { MessageBox.Show("SAP Connection is not established.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string query = @" SELECT 'Invoice', T0.""TransId"", T0.""DocNum"", T0.""DocDate"", T0.""BPLName"", T0.""CardCode"", T0.""CardName"", T0.""DocTotal"", T4.""U_UTL_IRN"" FROM ""Test"".""OINV"" T0 INNER JOIN ""CRD1"" T3 ON T0.""CardCode"" = T3.""CardCode"" INNER JOIN ""INV12"" T5 ON T5.""DocEntry"" = T0.""DocEntry"" LEFT JOIN ""Test"".""@AUTL_MDEXTH"" T4 ON T0.""DocEntry"" = T4.""U_UTL_BaseEntry"" WHERE T0.""CardCode"" = T3.""CardCode"" AND T0.""PayToCode"" = T3.""Address"" AND T3.""AdresType"" = 'B' AND T3.""GSTRegnNo"" <> '' AND T0.""DocEntry"" NOT IN ( SELECT A4.""U_UTL_BaseEntry"" FROM ""Test"".""@AUTL_MDEXTH"" A4 WHERE A4.""U_UTL_IST"" <> 'F' ) AND T5.""LocGSTN"" <> T5.""BpGSTN"" AND T0.""BPLId"" = '28'"; try { using (SAPbobsCOM.Recordset oRecordSet = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)) { oRecordSet.DoQuery(query); while (!oRecordSet.EoF) { string invoiceNumber = oRecordSet.Fields.Item("DocNum").Value.ToString(); Console.WriteLine($"Invoice: {invoiceNumber}"); oRecordSet.MoveNext(); } } } catch (Exception ex) { MessageBox.Show("Query Execution Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }Why can't you try this refactored code post your comments
Eliana BlakeRank #1,595 · 993 reputationPosted Mar 15, 2025, 3:49 AM EDT
It seems like the issue with `oCompany` being null and throwing an "Object Reference not set to an instance of the Object" error arises from the redeclaration of `oCompany` within the constructor of your `Form1` class.
In your code snippet, you have `SAPbobsCOM.Company oCompany = new SAPbobsCOM.Company();` within the constructor. This creates a new local variable `oCompany` within the constructor scope, which is distinct from the static class variable `oCompany` declared at the class level. As a result, the static `oCompany` remains null, leading to the error when you try to access it in the `PendingInvoice` method.
To resolve this issue, you should remove the type declaration `SAPbobsCOM.Company` when initializing `oCompany` within the constructor. Simply use `oCompany = new SAPbobsCOM.Company();` to initialize the static variable instead of redeclaring it. This way, you will be modifying the static `oCompany` variable, ensuring it is properly initialized and accessible throughout your class.
By making this adjustment, you should no longer encounter the null reference error, and your `PendingInvoice` method should be able to utilize the initialized `oCompany` instance successfully.
If you need further clarification or assistance, feel free to ask!