Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 output:

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



Error Message:

Quote:
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


What I have tried:

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

    }
}



And this is how i call the function in my form1.cs

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();
            }
        }
Posted
Updated 20-Feb-22 2:43am

1 solution

We can't tell - we would need your data and your code running to even being looking and we have no idea what that looks like.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900