Solved

object refrence not set to an instance of an object

Posted on 2011-03-10
2
249 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:patd1
  • 2
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35099160
Are you certain lines 12 and 13 are returning valid connection strings? If they return null, the calls to ToString() will fail.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35099180
P.S.

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now