PORCP1headerFields.FieldByName("RCPHSEQ").SetValue("0", false) || Returns "Object reference not set to an instance of an object"

Good day,

Please assist,

I have C# code to export data from my local DB to Sage. When inserting invoice headers I get "Object reference not set to an instance of an object"

I am fairly new to project and Never worked on sage before. I have a very narrow knowledge. 

See my code below. On the green highlighted that is where the error occurs.

public override void InsertInvoiceHeader()
{
       PORCP1header.Order = 1;
       PORCP1header.Order = 0;

      PORCP1headerFields.FieldByName("RCPHSEQ").SetValue("0", false); // Receipt Sequence Key
      PORCP1header.Init();
      PORCP1header.Order = 1;
      PORCP1detail1.RecordClear();
      PORCP1detail3.RecordClear();
      PORCP1detail4.RecordClear();
      PORCP1detail6.Init();
      PORCP1detail2.Init();
      sRcpSeq = PORCP1headerFields.FieldByName("RCPHSEQ").Value.ToString();
      PORCP1headerFields.FieldByName("RCPNUMBER").SetValue(base.dsHeaders.Tables[0].Rows[base.counterIH]["RCPNUMBER"].ToString(), true); // RECEIPT NUMBER
      PORCP1headerFields.FieldByName("VDCODE").SetValue(base.dsHeaders.Tables[0].Rows[base.counterIH]["VDCODE"].ToString(), true); // VENDOR
      PORCP1headerFields.FieldByName("PROCESSCMD").SetValue("1", false); // Process Command
      PORCP1header.Process();
      PORCP1headerFields.FieldByName("DATE").SetValue(base.dsHeaders.Tables[0].Rows[base.counterIH]["DATE"].ToString(), true); // DATE
      PORCP1headerFields.FieldByName("REFERENCE").SetValue(base.dsHeaders.Tables[0].Rows[base.counterIH]["REFERENCE"].ToString(), true); // REFERENCE
      PORCP1headerFields.FieldByName("DESCRIPTIO").SetValue(base.dsHeaders.Tables[0].Rows[base.counterIH]["DESCRIPTIO"].ToString(), true); // DESCRIPTION

}

  • Don't set that value, let Sage do it.  Don't set RCPNUMBER, either.

  • in reply to Jay Converse Acumen

    Hi Jay, 

    Still get the same error.

    you meant just do the fieldbyName without SetValue right?

    PORCP1headerFields.FieldByName("RCPHSEQ");

  • in reply to SIPHO MATSEME

    Yes, leave RCPHSEQ alone.  And it sounds like you haven't initialized PORCP1header, either. 

    Did you do this?

    public static AccpacSession session;

    public static AccpacDBLink a4wLink;
    public static AccpacDBLink a4wLinkRead;

    session = new AccpacSession();
    session.Init("", "XY", "XY1000", "62A");
    session.Open(ACCPACUSER, ACCPACPW, Properties.Settings.Default.AccpacCompany, DateTime.Today, 0, "");

    a4wLink = session.OpenDBLink(tagDBLinkTypeEnum.DBLINK_COMPANY, tagDBLinkFlagsEnum.DBLINK_FLG_READWRITE);
    a4wLinkRead = session.OpenDBLink(tagDBLinkTypeEnum.DBLINK_COMPANY, tagDBLinkFlagsEnum.DBLINK_FLG_READONLY);

    a4wLinkRead.OpenView("PO0700", out PORCP1header);

    etc.

    etc.

  • in reply to Jay Converse Acumen

    Yes, I do have that and PORCP1header is initialized 


    //===================================================
    // Open PO0700 Invoice View on Initialization
    //===================================================
    PORCP1header = base.mDBLinkCmpRW.OpenView("PO0700");
    PORCP1headerFields = PORCP1header.Fields;

  • in reply to SIPHO MATSEME

    Hi Jay, Managed to resolve the issue. PORCP1headerFields was null.

    Now I get this error at PORCP1detail9.Insert();

    if (base.dsDetailSoftCols.Tables[0].Rows[base.counterDSC]["DETAILSOFTCOLS"].ToString() == "1")
    {
    for (int scCol = 0; scCol < dsDetailSoftCols.Tables[1].Columns.Count; scCol++)
    {
    //========================================================
    // Assign each Optional Field Name and Value SEPERATELY
    //========================================================
    PORCP1detail3.RecordClear();
    PORCP1detail9.RecordGenerate(false);
    PORCP1detail9.Fields.FieldByName("OPTFIELD").SetValue(base.dsDetailSoftCols.Tables[1].Columns[scCol].ColumnName, false); // OPTIONAL FIELD NAME
    PORCP1detail9.Read(false);
    PORCP1detail9.Fields.FieldByName("VALIFTEXT").SetValue(base.dsDetailSoftCols.Tables[1].Rows[base.counterDSC][scCol].ToString(), false); // OPTIONAL FIELD VALUE
    // Update each Optional Field Name and Value
    PORCP1detail9.Insert();
    }
    }

  • in reply to SIPHO MATSEME

    Single step and examine the Errors object.