?
Solved

object refrence not set to an instance of an object

Posted on 2011-03-10
2
Medium Priority
?
266 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

755 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