Solved

object refrence not set to an instance of an object

Posted on 2011-03-10
2
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

705 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