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

asked on

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

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi Kenny;

the variable patientmpi has not been defined in the program.

DataType patientmpi;

Fernando
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
Avatar of kwh3856

ASKER

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-----

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

Avatar of kwh3856

ASKER

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

 
What is the data type of the underlying database column?
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
Avatar of kwh3856

ASKER

In the database, it is type unique.
Thanks
Kenny
 
Avatar of kwh3856

ASKER

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-----

                                }
 
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
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
Avatar of kwh3856

ASKER

Carl,
I still got the same error message with that line.
Thanks
Kenny
 
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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
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
Avatar of kwh3856

ASKER

Fernando,
Thank you very much.
Not a problem, always glad to help.  ;=)