Nullable object must have a value.

Posted on 2009-02-23
Last Modified: 2013-12-17
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 equals u.uid

                           join i in dceClinicalWorks.patientinfos on equals

                           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();


                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

Question by:kwh3856
    LVL 19

    Accepted Solution

    public static readonly DBNull Value

    try DBNull.Value instead of 0
    LVL 19

    Expert Comment

    also look here

    Nullable Types (C# Programming Guide)

    Author Closing Comment

    Thank you very much.
    LVL 62

    Expert Comment

    by:Fernando Soto
    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();


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    734 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

    23 Experts available now in Live!

    Get 1:1 Help Now