Solved

object refrence not set to an instance of an object

Posted on 2011-03-10
2
248 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 74

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

708 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

16 Experts available now in Live!

Get 1:1 Help Now