[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Nullable object must have a value.

Posted on 2009-02-23
4
Medium Priority
?
3,009 Views
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 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

0
Comment
Question by:kwh3856
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
weellio earned 2000 total points
ID: 23715702
public static readonly DBNull Value

try DBNull.Value instead of 0
0
 
LVL 19

Expert Comment

by:weellio
ID: 23715729
also look here

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

Author Closing Comment

by:kwh3856
ID: 31550304
weelio,
Thank you very much.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 23716089
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
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month19 days, 10 hours left to enroll

872 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