Search code examples
c#web-servicesmicrosoft-dynamicsnavisiondynamics-nav

Dynamics NAV Webservice Fails on 2nd Loop


I have created a C# program to read a pipe ("|") delimited file and create purchase invoices and lines. Essentially, I have it loop through each line, if the "Report ID" has not been used, it creates a header, then the line, if the header has been created, it skips the header creation and is supposed to add the subsequent line. However, when I reach my object assignment for the line, it errors with:

"ArgumentException was unhandled" "Must specify valid information for parsing the string."

The PIheader function works fine, so I have not included here. Please advise if more information/code is needed.

//Parse selected SAE File
SAEline[] sae = ParseSAE.Parse(file);

//Begin Analyzing Data
int saesize = sae.Length;
int i = 0;
List<string> cashIDs = new List<string>();
string paymentterms = "";
string invno = "";
string company = "";
string[] getcompany = new string[2];
string reportid = sae[i].ReportID;
int lineno = 0;
while(i < 10) //limit the loop for testing
//while (i < saesize)
{
    if (sae[i].ReportEntryPaymentCodeCode != "CBCP")
    {
        if (!cashIDs.Contains(reportid))
        {
            cashIDs.Add(reportid);
            getcompany = WebServices.GetCompany(sae[i].EmployeeID.ToUpper());
            paymentterms = sae[i].ReportEntryPaymentCodeCode;
            invno = WebServices.PIheader(getcompany[0], getcompany[1], 0, sae[i]);
            lineno = 0;
        }
        lineno = lineno + 10000;
        company = getcompany[0];
        lineno = WebServices.PIlines(invno, lineno, company, sae[i]);
    }
    i++;
}

The WebService.cs contains:

//Web Service Client
        PurchLines.PurchLines_PortClient piClient =
            new PurchLines_PortClient((System.ServiceModel.Channels.Binding)basicHttpBindingNTLM,
                new EndpointAddress("URL" + company + "/Page/PurchLines"));

        //Conditional variables
        string joblinetype = "";
        string qty = "";


        if  (sae.ReportEntryCustom1 == "Billable")
        {
            joblinetype = "3";
        }

        if (sae.BusinessDistance == "")
        {
            if (sae.ReportCustom2 == "")
            {
                qty = "1";
            }
            else
            {
                qty = sae.ReportCustom2;
            }
        }
        else
        {
            qty = sae.BusinessDistance;
        }

        string unitcost = (Convert.ToDecimal(sae.ReportEntryApprovedAmount)/Convert.ToDecimal(qty)).ToString();

        //Line Creation
        PurchLines.PurchLines line = new PurchLines.PurchLines()
        {
            No = sae.JournalAccountCode,
            Line_No = Convert.ToInt16(lineno),
            Line_NoSpecified = true,
            Job_Line_TypeSpecified = true,
            Job_Line_Type = (PurchLines.Job_Line_Type) (Enum.Parse(typeof (PurchLines.Job_Line_Type), joblinetype)),
            QuantitySpecified = true,
            Quantity = Convert.ToDecimal(qty),
            TypeSpecified = true,
            Type = (PurchLines.Type) (Enum.Parse(typeof (PurchLines.Type), "1")),
            Direct_Unit_CostSpecified = true,
            Direct_Unit_Cost = Convert.ToDecimal(unitcost),
            Job_Unit_PriceSpecified = true,
            Job_Unit_Price = Convert.ToDecimal(unitcost),
            Job_No = sae.ReportEntryCustom5,
            Job_Task_No = sae.ReportEntryCustom6,
            Document_TypeSpecified = true,
            Document_Type = (PurchLines.Document_Type)(Enum.Parse(typeof(PurchLines.Document_Type),"2")),
            Document_No = invno
        };

        piClient.Create(ref line);
        PurchLines.Create_Result result = new PurchLines.Create_Result(line);
        int lin = result.PurchLines.Line_No;
        return lin;
    }

Solution

  • I realized that I didn't assign a value to joblinetype in the event that it is not "Billable", so the webservice was unable to Parse the blank string

    Job_Line_Type = (PurchLines.Job_Line_Type) (Enum.Parse(typeof (PurchLines.Job_Line_Type), joblinetype)),