Link to home
Start Free TrialLog in
Avatar of patd1
patd1Flag for United States of America

asked on

object refrence not set to an instance of an object

I am getting this error, but do not understand why. please review this sample code for class and suggest corrections.

calling method is as follows:
AddOrderToMySystem OrderAdder = new AddOrderToMySystem();//exception thrown on this line
        int AccessionID = OrderAdder.AccessionOrder(patientID, facilityID, this.userID, doctorID, messageID, messageControlID);

Open in new window


Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Transactions;


public class AddOrderToMySystem
{
    private static readonly string AConnection = ConfigurationManager.ConnectionStrings["APVX"].ToString();
    private static readonly string Gonnection = ConfigurationManager.ConnectionStrings["GeorgiaCancer"].ToString();
    private struct TestsOrdered
    {
        public string HostCode, TestName, HostType, CSICaseType;
        public int CSICaseTypeID, CaseGroupID, TestID, PanelID;
    }

    private struct SpecimenInfo
    {
        public string HostCode, ExtSpecimenID, SpecimenType, BodySite, Additives, SpecimenCollectionDateTime;
        public int CaseGroupID;
    }
    private Dictionary<int, string> HostCodesOrdered = new Dictionary<int, string>();
    private Dictionary<int, string> CasesOrdered = new Dictionary<int, string>(); 
    private Dictionary<int, int> CaseIdsOrdered = new Dictionary<int, int>();
    private Dictionary<int, int> CaseTypeIDsOrdered = new Dictionary<int, int>(); 
    private Dictionary<int, string> CaseNumbersToOrder = new Dictionary<int, string>(); 
    private Dictionary<int, int> SpecimenIDs = new Dictionary<int, int>();

    private List<TestsOrdered> ListTestsOrdered = new List<TestsOrdered>();
    private List<SpecimenInfo> SpecimenList = new List<SpecimenInfo>();
    private int OrderID = 0;
    //=========================================================

    public int AccessionOrder(int patientID, int facilityID, int userID, int doctorID, int messageID, int messageControlID)
    {
        int retVal = 0;
        try
        {
            // Create the TransactionScope to execute the commands, guaranteeing that all commands can commit or roll back as a single unit of work.
            using (TransactionScope scope = new TransactionScope())
            {
                AddOrder(patientID, facilityID, userID, doctorID, messageID, messageControlID);
                //Commit the transaction. If an exception has been thrown the transaction is rolled back.
                scope.Complete();
            }
        }
        catch (TransactionAbortedException ex)
        {
            retVal = 0;
            throw new Exception("Transaction Aborted when attempting to accession Message Control ID " + messageControlID + ".", ex);
        }
        retVal = OrderID;
        return retVal;
    }

    private void PrepareData(int msgID)
    {
        //Get Tests from the client Database
        ListTestsOrdered = GetTestInfoByMsgID(msgID);

        //Populate HostCodesOrdered, CasesOrdered and CaseTypeIDsOrdered; Common key CaseGroupID;
        GetCasesByMsgID(ListTestsOrdered);

        //Populate Struct SpecimenInfo       
        SpecimenList = GetSpecimenList(msgID);
    }

    private void AddOrder(int patientID, int FacilityID, int userID, int doctorID, int messageID, int MessageControlID)
    {
        //0. PREPARE DATA
        PrepareData(messageID);

        //1. Add a row to Orders.Acession
        OrderID = WriteAcession(patientID, FacilityID, userID);       

        

        //2.GET NEXT CASENO IN APVX
        foreach (KeyValuePair<int, int> kvp in CaseTypeIDsOrdered)
        {
            string CaseNo = GetNextCaseNo(kvp.Value);
            if (!String.IsNullOrEmpty(CaseNo))
                CaseNumbersToOrder.Add(kvp.Key, CaseNo);
        }

        //3. ADD CASES
        foreach (KeyValuePair<int, string> kvp in CaseNumbersToOrder)
        //For each case: add case, add specimen, link case to HL7 order, add tests and components
        {
            //Add Case    
            string CaseNo = kvp.Value;
            if (CaseTypeIDsOrdered.ContainsKey(kvp.Key)) // True
            {
                string DesignatorCode = CasesOrdered[kvp.Key];
                int CaseId = AddCase(OrderID, DesignatorCode, CaseNo, userID);
                CaseIdsOrdered.Add(kvp.Key, CaseId);
            }
            //Get specimen for this case from SpecimenList
            SpecimenInfo SpecimenForThisCase = new SpecimenInfo();
            foreach (SpecimenInfo SP in SpecimenList)
            {
                if (SP.CaseGroupID == kvp.Key)
                {
                    SpecimenForThisCase.ExtSpecimenID = SP.ExtSpecimenID.ToString().Trim();
                    SpecimenForThisCase.SpecimenType = SP.SpecimenType.ToString().Trim();
                    SpecimenForThisCase.Additives = SP.Additives.ToString().Trim();
                    SpecimenForThisCase.BodySite = SP.BodySite.ToString().Trim();
                    SpecimenForThisCase.SpecimenCollectionDateTime = SP.SpecimenCollectionDateTime.ToString().Trim();
                }
            }
            
            //4. Add specimen for this
            int SpecimenID = AddSpecimen(userID, CaseNo, SpecimenForThisCase);
            SpecimenIDs.Add(kvp.Key, SpecimenID);

            
        }//end for each case


        //6A. Add Tests for the accession
        foreach (TestsOrdered Test in ListTestsOrdered)
        {
            
        }


     
    }
    private List<TestsOrdered> GetTestInfoByMsgID(int MsgID)
    {
        TestsOrdered TO = new TestsOrdered();
        List<TestsOrdered> retVal = new List<TestsOrdered>();
        

        return retVal;
    }

    private void GetCasesByMsgID(List<TestsOrdered> TestsList)
    {
        int PreviousCaseGroupID = 0;
        foreach (TestsOrdered TestInfo in TestsList)
        {
            string HostCode = TestInfo.HostCode;
            int CaseGroupID = TestInfo.CaseGroupID;
            string CSICaseType = TestInfo.CSICaseType;
            int CaseTypeID = TestInfo.CSICaseTypeID;
            if (CaseGroupID != PreviousCaseGroupID)
            {
                HostCodesOrdered.Add(CaseGroupID, HostCode);
                CasesOrdered.Add(CaseGroupID, CSICaseType);
                CaseTypeIDsOrdered.Add(CaseGroupID, CaseTypeID);
            }
        }
    }

    private List<SpecimenInfo> GetSpecimenList(int MessageID)
    {
        SpecimenInfo Specimen = new SpecimenInfo();
        List<SpecimenInfo> SpecimenList = new List<SpecimenInfo>();
        
        return SpecimenList;
    }

    private int WriteAcession(int PID, int FID, int UID)
    {
        int AID = 0;      

        return AID;
    }

    private string GetNextCaseNo(int CaseTypeID)
    {
        string CaseNum = string.Empty;        

        return CaseNum;
    }

    private int AddSpecimen(int UID, string caseNo, SpecimenInfo SP)
    {
        int SpecimenID = 0;

        return SpecimenID;
    }

    private int AddCase(int AccessionID, string DesignatorCode, string CaseNo, int UID)
    {
        int CaseID = 0;        

        return CaseID;
    }

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
P.S.

Indexing the ConnectionStrings collection returns a string, so the ToString() calls are really redundant anyways.