Link to home
Start Free TrialLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

Nullable object must have a value.

I have a variable defined as an int and then assign it a value of 0.  Next I read a field of data from a table and determine if it is null or has a value.  If it has a value I assigne it to the variable.  I keep getting this error message even after I initialze the variable with 0.  What am I missing?

Here is the code:
class Program
    {
 
        // Global Variables Class 
        internal static class MyGlobalVars
        {
            public static int docnpi;
            
            public static Guid initGuid;
        }
 
 
        static void Main(string[] args)
        {
 
            MyGlobalVars.docnpi = 0;----------------------------  Variable Initilized HERE   -------
 
 
 
 
            DataClasses1DataContext dcChartRelay = new DataClasses1DataContext();
 
            DataClasses2DataContext dceClinicalWorks = new DataClasses2DataContext();
 
 
 
 
 
 
 
            // Query to get all patients from the Patients table
            // that have not been flagged as imported
            var patients = from p in dceClinicalWorks.patients
                           where p.Imported == null
                           join d in dceClinicalWorks.doctors on p.doctorId equals d.doctorID
                           join u in dceClinicalWorks.users on p.pid equals u.uid
                           join i in dceClinicalWorks.patientinfos on p.pid equals i.pid
                           select new { d, u, i, p };
 
 
 
 
 
 
            // Process each row of the result set in the foreach loop
            foreach (var patient in patients)
            {
 
                // ------- eClinical Tables
                patientinfo myPatientInfo = new patientinfo();
                user myUser = new user();
 
 
                // ------- Chart Relay Tables
                MPI myMPI = new MPI();
                PATIENT myCRpatient = new PATIENT();
                PATIENT_PHONE_NUMBER myPatPhones = new PATIENT_PHONE_NUMBER();
                Patient_Address myPatAddresses = new Patient_Address();
                PATIENTS_ATTENDED_BY_DOCTOR myPatAttByDoc = new PATIENTS_ATTENDED_BY_DOCTOR();
                PATIENT_IN myPatIns = new PATIENT_IN();
                EMPLOYER myPatEmployers = new EMPLOYER();
                BillingAlert myPatBillAlerts = new BillingAlert();
                EligibilityStatus myPatElgStatus = new EligibilityStatus();
 
 
 
 
                // ----------------Write the MPI Record 
 
 
 
 
                MyGlobalVars.initGuid = Guid.NewGuid();
                myMPI.MPI1 = MyGlobalVars.initGuid;
 
                //myMPI.NPI = Convert.ToInt32(patient.d.NPI.ToString());
 
 
 
 
                MyGlobalVars.docnpi = patient.d.NPI != null ? (int)myMPI.NPI : 0;-----  ERROR HERE-----------------------------------

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of William Elliott
William Elliott
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
also look here

Nullable Types (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
Avatar of kwh3856

ASKER

weelio,
Thank you very much.
Hi Kenny;

Not sure on this but try this.

Change this line of code in the MyGlobalVars class:

public static int docnpi;

to this:

public static int? docnpi;


Change this line at the end of the code:

MyGlobalVars.docnpi = patient.d.NPI != null ? (int)myMPI.NPI : 0;

To this:

MyGlobalVars.docnpi = patient.d.NPI.GetValueOrDefault();

Fernando