Why does restclient.executeasync<list<T>>() return null for an array-valued JSON response?

xtian

New member
Joined
Feb 20, 2022
Messages
1
Programming Experience
Beginner
I need help with the project I am currently working on. Please bare with me, I'm not good at coding specially with c# this is my first attempt.

I have a button the will pull the data from webservice. Saving of data to database will be triggered with the same button. I have manage to display the string via tutorial and now stuck on saving I don't know where I went wrong it keeps on throwing error that **Value cannot be null ** I have provided the error message for your reference and the json output.


JSON:
[{"NAME":"FOLDER_NAME","LOW":"0000000376_0000000385","HIGH":"SAMPLE\\376_AGENPATH\\385_NAMEPATH, INC","ERDAT":"0000-00-00","AEDAT":"0000-00-00"}]


This is my CIP.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestSharp.Authenticators;
using System.IO;
using RestSharp;
using System.Text.Json;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml.Linq;

namespace SAPSO
{
    class CIP
    {
        public class CIPOutput
        {
            public string NAME { get; set; }
            public string HIGH { get; set; }
            public string LOW { get; set; }
            public DateTime ERDAT { get; set; }
            public DateTime AEDAT { get; set; }

        }

        RestClient client;
        RestRequest request;
        
        public CIP ()
        {
            client = new RestClient(@"http://privateurl:8000/");
            client.Authenticator = new HttpBasicAuthenticator("user", "pass");


        }

       public List<CIPOutput>
        GetCIPOutputs(DateTime start, DateTime end)
        {
            request = new RestRequest("sap/ez/zcip", Method.Get);
            request.AddParameter("sap-client", "200");
            request.AddParameter("start", "");
            request.AddParameter("end", end.ToString("yyyyMMdd"));
            var output = client.ExecuteAsync<List<CIPOutput>>(request);
            var o = output.Result.Data;
            return o;


        }

        public void ProcessOutput(DateTime start, DateTime end)
        {
            var output = GetCIPOutputs(start, end);

            if (output == null || output.Count < 1)
                {
                Console.WriteLine("Null");
            }

            else
            {

                Console.WriteLine(output.ToString());
              
            }

            var xmlPath = new XElement("CIPS",
               from CIPPath in output
               select new XElement("CIP",
                      new XElement("NAME", CIPPath.NAME),
                      new XElement("HIGH", CIPPath.HIGH),
                      new XElement("LOW", CIPPath.LOW),
                      new XElement("ERDAT", CIPPath.ERDAT),
                      new XElement("AEDAT", CIPPath.AEDAT)
                      ));


            insertCIP(xmlPath.ToString(), "InsertCIPpath");

        }

        //insert to db
        int insertCIP(string xml, string strdproc)
        {
            int row = 0;
            try
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BMSSAP"].ConnectionString))
                {
                    using(SqlCommand cmd = new SqlCommand(strdproc))
                    {
                        cmd.Connection = con;
                        cmd.CommandTimeout = 3000;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@xmldata", xml);
                        con.Open();
                        row = cmd.ExecuteNonQuery();
                        con.Close();
                    }

                }
            }

            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return row;
        }

    }
}


This is how I call the function:
void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                _backgroundWorkerThread = Thread.CurrentThread;
                SAP svc = new SAP();
                CIP cip = new CIP();
                cip.ProcessOutput(dte_from.Value, dte_to.Value);
                
                
            }
            catch (ThreadAbortException)
            {
                e.Cancel = true;
                m_oWorker.Dispose();
                Thread.ResetAbort();
            }
        }






This is the error I'm getting


System.ArgumentNullException HResult=0x80004003 Message=Value cannot be null. Parameter name: source Source=System.Core
StackTrace: at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source) at SAPSO.CIP.ProcessOutput(DateTime start, DateTime end) in C:\prjname\fldername\GET\SAPSO\CIP.cs:line 58 at SAPSO.Form1.m_oWorker_DoWork(Object sender, DoWorkEventArgs e) in C:\prjname\fldername\GET\SAPSO\Form1.cs:line 103 at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

This exception was originally thrown at this call stack: [External Code] SAPSO.CIP.ProcessOutput(System.DateTime, System.DateTime) in CIP.cs SAPSO.Form1.m_oWorker_DoWork(object, System.ComponentModel.DoWorkEventArgs) in Form1.cs
 
Have you tried asking the forum that supports RestSharp?
 

Latest posts

Back
Top Bottom