Use of unassigned local variable

I am trying to assign a variable a value from a table and I get this error message on the value from the table.  What would create this error message.
if (type == "PATIENT")
                            {
                                
 
 
                                // Lookup Patient in MPI Table and obtain MPI of Patient
                                var patientlookup = from MPIREC in db.MPIs
                                                    where recordMPI.MPI1 == MyGlobalVars.initGuid &&
                                                    recordMPI.NPI == MyGlobalVars.docnpi
                                                    select new { MPIREC };
 
 
                                if (patientlookup.Count() == 0)
                                {
                                    // No Record found in NPI - Must be new patient
                                }
                                else
                                {
                                    // Found patient get MPI 
                                    Guid patientmpi;
                                    patientmpi = recordMPI.MPI1;-----squiggly under recordMPI-----
 
                                }
 
                                // Lookup Patient in Patient table based on MPI
                                var patientdemolookup = from PATIENTDEMO in db.PATIENTs
                                                        where recordPatient.MPI == MyGlobalVars.initGuid ;-----squiggly under  recordPatient-------------------
                                                    select new { PATIENTDEMO };

Open in new window

kwh3856Asked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi Kenny;

Change the top of the function UpdateTable from this:

        private static void UpdateTable(DataClasses1DataContext db, Object record)
        {

            MPI recordMPI;                      // You need to define the variables here
            PATIENT recordPatient;        // You need to define the variables here

To This:

        private static void UpdateTable(DataClasses1DataContext db, Object record)
        {

            MPI recordMPI = null;                      // You need to define the variables here
            PATIENT recordPatient = null;              // You need to define the variables here

It is complaining about that the variable were not initialize before being used. This should get you through this issue.

Fernando
0
 
Fernando SotoRetiredCommented:
Hi Kenny;

the variable patientmpi has not been defined in the program.

DataType patientmpi;

Fernando
0
 
Fernando SotoRetiredCommented:
Hi Kenny;

Remember if patientmpi is going to be used outside of the if statement define it as shown below. I looked at the MPI db and did not see a record MPI1 so cannot tell you what type to place before patientmpi in the code snippet below.

private static void UpdateTable(DataClasses1DataContext db, Object record)
{
   
    MPI recordMPI;
    PATIENT recordPatient;
    DataType patientmpi;

Fernando
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
kwh3856Author Commented:
Fernando,
I am confused.  I thought I had defined patientmpi right above the line with the error.
 
if (patientlookup.Count() == 0)
                                {
                                    // No Record found in NPI - Must be new patient
                                }
                                else
                                {
                                    // Found patient get MPI
                                    Guid patientmpi;-----------------------------------------------------------------------patientmpi defined as GUID-----------
                                    patientmpi = recordMPI.MPI1;-----squiggly under recordMPI-----

                                }
 
0
 
Carl TawnSystems and Integration DeveloperCommented:
Its complaining because you are trying to a straight assignment to a Guid type that does not have an initial value.
Depending on how you are storing the guid value in your database, you probably want something more like:

     Guid patientmpi = new Guid(recordMIP.MPI1);

0
 
kwh3856Author Commented:
Carl,
When I tried that I got these two error messages
The best overloaded method match for 'System.Guid.Guid(byte[])' has some invalid arguments
Argument '1': cannot convert from 'System.Guid' to 'byte[]'
Thanks
Kenny

 
0
 
Carl TawnSystems and Integration DeveloperCommented:
What is the data type of the underlying database column?
0
 
Fernando SotoRetiredCommented:
Your right Kenny, I missed that. carl_tawn post is correct but do not define it in the if statement because it looks as you will be using it outside of the if code block and it will not be available when you need it. You need to define it as I showed in the last post, like this:

private static void UpdateTable(DataClasses1DataContext db, Object record)
{
   
    MPI recordMPI;
    PATIENT recordPatient;
    DataType patientmpi;

Now depending on the data type of MPI1 in the MPI table change DataType in code above to that type. so if the data type in the db is Guid then it should look like this:

private static void UpdateTable(DataClasses1DataContext db, Object record)
{
   
    MPI recordMPI;
    PATIENT recordPatient;
    Guid patientmpi;

Fernando
0
 
kwh3856Author Commented:
In the database, it is type unique.
Thanks
Kenny
 
0
 
kwh3856Author Commented:
Fernando,
I have done that and now I am back to the same error that I started with.
It seems the error has more to do with recordMPI than the patientmpi variable.  It is like it does not understand that I am trying to pull the value out of the recordMPI table.  When I type in recordMPI.   intellisense definately shows me the available fields such as MPI.  Yet the error message seems to say it does not know it is a field.  What I am not understanding here?
Thanks
Kenny
 
 
if (patientlookup.Count() == 0)
                                {
                                    // No Record found in NPI - Must be new patient
                                }
                                else
                                {
                                    // Found patient get MPI
                                    Guid patientmpi;
                                    patientmpi = recordMPI.MPI1;-----squiggly under recordMPI-----

                                }
 
0
 
Carl TawnSystems and Integration DeveloperCommented:
It's the fact that you are trying to assign to an uninitialized Guid that it is complaining about. You to use one of the constructors of the Guid type to convert your database value into something recognisable.

I haven't done much with Linq so i'm not sure if there will be a ToString() method or not. But you basically want to get your Db value into a string and pass that to the guid constructor:

    Guid patientmpi;
    patientmpi = new Guid(recordMPI.MPI1.ToString());      // may need tweaking a little
0
 
Fernando SotoRetiredCommented:
Kenny;

A uniqueidentifier is a 16 byte Guid value then the following should work. Remember to pull the definition of patientmpi out of the if statement if patientmpi will be used outside if the if code block.

I am leaving now for a meeting I have and do not know when I will return, sorry.

if (patientlookup.Count() == 0)
{
    // No Record found in NPI - Must be new patient
}
else
{
    // Found patient get MPI
    Guid patientmpi;
    patientmpi = new Guid(recordMPI.MPI1);
}

Fernando
0
 
kwh3856Author Commented:
Carl,
I still got the same error message with that line.
Thanks
Kenny
 
0
 
Fernando SotoRetiredCommented:
Hi Kenny;

I just tried this code and it worked fine. Because MPI1 is a uniqueidentifier / GUID it does not need to be cast or converted to a Guid.

// Define the variable
Guid patientmpi;

if (patientlookup.Count() == 0)
{
    // No Record found in NPI - Must be new patient
}
else
{
    // Found patient get MPI
    patientmpi = recordMPI.MPI1;
}

Fernando
0
 
kwh3856Author Commented:
Fernando,
Thank you very much.
0
 
Fernando SotoRetiredCommented:
Not a problem, always glad to help.  ;=)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.