-2

I am trying to create multiple threads based on Data tables rows, if there are 10 rows in a table, it creates 10 threads. I keep getting this error "Value cannot be null.Parameter name: value". What am I missing in this?

I am getting error inside Savedata method at first catch statement that is "inner excep" comment.

try
{
    DataProvider DPrvdr = new DataProvider();
    DPrvdr.ConnectionString = strConMAS500; // read only

    //getting all the sales order 
    DataTable dt = DPrvdr.GetDataSetSProc("uspMAS500SOSER_GetSO").Tables[0];
    List<DataTable> lstDt = SplitTable(dt, MaxSOinThread); // Split the Data table as per MaxSO in Threads - Currently it is 1

    if (lstDt != null)
    {
        Parallel.ForEach(lstDt, (currentdt) =>
        {
            hashSOTbl = Savedata(currentdt);
        });
    }
}
catch (Exception ex)
{
    LogMessage("Error " + ex.Message);
}


    protected Hashtable Savedata(DataTable dt)
    {
        SOEntityBOLWS_Client c = new SOEntityBOLWS_Client();
        Hashtable hashSODetails = new Hashtable();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            try
            {
                //initializing the SO object
                SOEntityForClient.SalesOrder s = c.GetNewSO(dt.Rows[i]["CreatedBy"].ToString(), dt.Rows[i]["SessionID"].ToString());
                string SONO = string.Empty;
                s.CustID = dt.Rows[i]["CustID"].ToString().Trim();
                s.CompanyID = dt.Rows[i]["CompanyID"].ToString().Trim();
                s.CustPONo = dt.Rows[i]["CustPONo"].ToString();                 

                s.CustClassID = (dt.Rows[i]["CustClassID"].ToString());
                s.SalesSourceID = (dt.Rows[i]["SalesSourceID"].ToString());
                s.CreateUserID = (dt.Rows[i]["CreatedBy"].ToString());
                int rowkey = Convert.ToInt32(dt.Rows[i]["RowKey"].ToString());                  

                DataProvider DPrvdrheaderComment = new DataProvider();
                DPrvdrheaderComment.ConnectionString = strConMAS500; // read only
                SqlParameter[] sqlHeaderComment = new SqlParameter[2];
                sqlHeaderComment[0] = new SqlParameter("@DocOwnerKey", rowkey);
                sqlHeaderComment[1] = new SqlParameter("@ParentDocType", 801);//801 for header comment

                //getting Comment               
                DataTable dtHeaderComment = DPrvdrheaderComment.GetDataSetParams("uspMAS500SOSER_GetSOComments", sqlHeaderComment).Tables[0];
                if (dtHeaderComment.Rows.Count > 0)
                {
                    s.Comments = new List<SOEntityForClient.Comment>();
                    for (int m = 0; m < dtHeaderComment.Rows.Count; m++)
                    {

                        SOEntityForClient.Comment cm = new SOEntityForClient.Comment();
                        cm.CommentText = dtHeaderComment.Rows[m]["CommentText"].ToString();
                        cm.ExtCmntID = dtHeaderComment.Rows[m]["ExtCommentID"].ToString();
                        cm.ParentDocType = 801;
                        cm.SeqNo = m + 1;
                        s.Comments.Add(cm);
                    }
                }                  


                DataProvider DPrvdrLine = new DataProvider();
                DPrvdrLine.ConnectionString = strConMAS500; // read only
                SqlParameter[] sqlline = new SqlParameter[1];
                sqlline[0] = new SqlParameter("@RowKey", rowkey);           

                DataTable dtLine = DPrvdrLine.GetDataSetParams("uspMAS500SOSER_GetSOLine", sqlline).Tables[0];  

                for (int k = 0; k < dtLine.Rows.Count; k++)
                {
                    SOEntityForClient.SOLine l = new SOEntityForClient.SOLine();
                    l.ItemID = dtLine.Rows[k]["ItemID"].ToString();
                    //l.Description = dtLine.Rows[k]["Description"].ToString();
                    l.QtyOrd = Convert.ToDecimal(dtLine.Rows[k]["QtyOrd"].ToString());
                    l.UnitMeasID = dtLine.Rows[k]["UnitMeasID"].ToString();
                    l.UnitPrice = Convert.ToDecimal(dtLine.Rows[k]["UnitPrice"].ToString());
                    l.TradeDiscPct = Convert.ToDecimal(dtLine.Rows[k]["TradeDiscPct"].ToString());
                    l.WhseID = dtLine.Rows[k]["WarehouseID"].ToString();
                    l.PromiseDate = DateTime.Parse(dt.Rows[i]["DfltPromDate"].ToString());
                    l.RequestDate = DateTime.Parse(dt.Rows[i]["DfltRequestDate"].ToString());
                    l.ShipDate = DateTime.Parse(dt.Rows[i]["DfltShipDate"].ToString());                      

                    DPrvdrLineComment = new DataProvider();
                    DPrvdrLineComment.ConnectionString = strConMAS500; // read only
                    SqlParameter[] sqlComment = new SqlParameter[2];
                    sqlComment[0] = new SqlParameter("@DocOwnerKey", (dtLine.Rows[k]["SOLineKey"].ToString()));
                    sqlComment[1] = new SqlParameter("@ParentDocType", 802);//802 for line comment

                    //getting Comment
                    DataTable dtLineComment = DPrvdrLineComment.GetDataSetParams("uspMAS500SOSER_GetSOComments", sqlComment).Tables[0];
                    if (dtLineComment.Rows.Count > 0)
                    {
                        l.SOLineComments = new List<SOEntityForClient.Comment>();
                        for (int m = 0; m < dtLineComment.Rows.Count; m++)
                        {
                            SOEntityForClient.Comment cm = new SOEntityForClient.Comment();
                            cm.CommentText = dtLineComment.Rows[m]["CommentText"].ToString();
                            cm.ExtCmntID = dtLineComment.Rows[m]["ExtCommentID"].ToString();
                            cm.ParentDocType = 802;//802 for line comment
                            cm.SeqNo = m + 1;
                            l.SOLineComments.Add(cm);
                        }
                    }
                    //Save SO line.
                    s.LineItems.Add(l);                       
                }

                try
                {
                    //checking line count
                    if (s.LineItems.Count != 0)
                    {
                        int UpdateProcessStatus = 0;
                        try
                        {
                            string warning = c.SaveSO((dt.Rows[i]["CreatedBy"].ToString()), (dt.Rows[i]["CompanyID"].ToString().Trim()), dt.Rows[i]["TranNo"].ToString(), "801", s, dt.Rows[i]["SessionID"].ToString(), "OverrideDate:true");

                            if (warning.Contains("Saved"))
                            {
                                hashSODetails.Add(dt.Rows[i]["TranNo"].ToString(), dt.Rows[i]["CompanyID"].ToString());
                                UpdateProcessStatus = 1;
                            }
                            else
                            {
                                UpdateProcessStatus = 0;
                            }


                            DPrvdr = new DataProvider();
                            DPrvdr.ConnectionString = strConMAS500; 
                            SqlParameter[] sqlParam = new SqlParameter[4];

                            sqlParam[0] = new SqlParameter("@Tranno", dt.Rows[i]["TranNo"].ToString()); 
                            sqlParam[1] = new SqlParameter("@Message", warning); 
                            sqlParam[2] = new SqlParameter("@UpdateProcessStatus", UpdateProcessStatus); 
                            sqlParam[3] = new SqlParameter("@CompanyID", (dt.Rows[i]["CompanyID"].ToString())); 

                            DPrvdr.InsertDeleteUpdateDataStrProc("uspMAS500SOSER_SaveSOStatus", sqlParam);                               
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogMessage("Error while calling web service for Row Key : " + (dt.Rows[i]["RowKey"].ToString()) + " inner excep : " + ex.Message);
                }
            }

            catch (Exception ex)
            {
                LogMessage("Error while calling web service for Row Key : " + (dt.Rows[i]["RowKey"].ToString()) + " outter excep : " + ex.Message);
            }
        }
        return hashSODetails;
    }
user3518472
  • 23
  • 1
  • 8
  • 5
    Please show the complete stack trace - that's usually much more helpful than just the error message. Otherwise we don't know which method is throwing the exception. – Jon Skeet Feb 06 '19 at 11:39
  • 1
    well... which line is throwing? and are you sure that `SplitTable` isn't returning one of the elements as a `null`? – Marc Gravell Feb 06 '19 at 11:39
  • For me it looks like you have a collection that is null and try to do something on it e.g. Linq. It's all I can say without more info about your error. – Pati K Feb 06 '19 at 11:46
  • A bit offtopic: is it a good idea to create that many threads? – Bogdan Doicin Feb 06 '19 at 11:49
  • Are DataTables/DataRows even thread safe? – brijber Feb 06 '19 at 11:50
  • @brijber No, they aren't – Camilo Terevinto Feb 06 '19 at 11:51
  • I Guess problem in data not in parallel foreach, data has a null value for column or parameter that config notnull= true. – Yakamuz Feb 06 '19 at 11:53
  • `Parallel.ForEach` does not create a thread for every item, it is dependent on how many cores you have and the task scheduler so you don't need to split things into chunks of max threads but you do have to consider thread safety. – Crowcoder Feb 06 '19 at 12:03
  • @Crowcoder - Thats good to know. I thought Parallel.Foreach will run each threads desperately. What you mean by Thread safety? – user3518472 Feb 06 '19 at 12:34
  • I mean [this](https://stackoverflow.com/questions/261683/what-is-meant-by-thread-safe-code) – Crowcoder Feb 06 '19 at 13:06

1 Answers1

0

DataTable dt = new DPrvdr.GetDataSetSProc("uspMAS500SOSER_GetSO").Tables[0];

Emeke Joshua
  • 11
  • 1
  • 3