• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2078
  • Last Modified:

Embedded statement cannot be a declaration or labeled statement

I have no idea what this means or why it is occuring.  Can someone please enlighten me?

Thanks in advance.

Here is my code.
// Lookup the patient in the MPI table by the Provider NPI and Entity System
                            // acct number.  If exist, obtain MPI from record.
                            // If the MPI was obtained, update the record in the Chart Relay patient table
                            //  If no record was found in the MPI table, insert a new record into the 
                            // Chart Relay patient table.
                            // If it exist in the MPI table
 
                            // Then one if statement for each table type
                            if (type == "PATIENT")
                                 
                                 ---------  Squiggly line right under DataClasses1DataContext-----------ERROR HERE
                                 DataClasses1DataContext dcChartRelay2 = new DataClasses1DataContext();
 
                                 MPI myMPI2 = new MPI();
                                 patient myECPatient2 = new patient();
                                 doctor myECdoctor2 = new doctor();
                                
                                 var patientlookup = from MPIREC in dcChartRelay2.MPIs
                                 where myMPI2.EntitySystemAcctNum == myECPatient2.pid &&
                                 myMPI2 = myECdoctor2.NPI
                                 select new { MPIREC };
 
                                 Guid getMPI;
                                 getMPI = myMPI2.MPI1;
 
 
                                 var demographlookup = from CRpatient in dcChartRelay2.PATIENTs
                                                       where CRpatient.MPI = getMPI
                                                       select (CRpatient);
        
                            
                            if(demographlookup.Count() ==0)
                            {
                               // No Record was found, Insert new record
                               db.PATIENTs.InsertOnSubmit(record as PATIENT); 
                            }
                            else
                            {
                                // Record was found based on MPI search, Upate current record
                                db.SubmitChanges();
                            }

Open in new window

0
kwh3856
Asked:
kwh3856
  • 3
  • 2
1 Solution
 
naspinskiCommented:
you should declare the context before the if, though you wont have to initiliaze it until after
DataClasses1DataContext dcChartRelay2;
if (type == "PATIENT")
  dcChartRelay2 = new DataClasses1DataContext();

Open in new window

0
 
kwh3856Author Commented:
naspinski,
Thanks for answing my question but it still gives me the same error message.  Here is what I did....
 

DataClasses1DataContext dcChartRelay2;
if (type == "PATIENT")
DataClasses1DataContext dcChartRelay2 = new DataClasses1DataContext();
MPI myMPI2 = new MPI();
patient myECPatient2 = new patient();
doctor myECdoctor2 = new doctor();
0
 
naspinskiCommented:
you added in an extra 'DataClasses1DataContext' that you dont need
DataClasses1DataContext dcChartRelay2; 
if (type == "PATIENT") 
dcChartRelay2 = new DataClasses1DataContext(); //notice this is just the variable name
MPI myMPI2 = new MPI(); 
patient myECPatient2 = new patient(); 
doctor myECdoctor2 = new doctor(); 

Open in new window

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
kwh3856Author Commented:
naspinski,
Thank you very much.
0
 
naspinskiCommented:
happy to help :)
0
 
Fernando SotoCommented:
Hi Kenny;

The function UpdateTable(...) should look like this, see the code snippet, having one case statement to cover all table types and not one try/catch for each table type.

The reason for the error you are getting I believe is do to possibly an if statement that contains one statement and that statement is to create a DataContext as shown below. It may not be blocked correctly, not having { ... }, missing { and having }, I am not sure at this point.

==================================================================
// Then one if statement for each table type
if (type == "PATIENT")
     
     DataClasses1DataContext dcChartRelay2 = new DataClasses1DataContext();

     MPI myMPI2 = new MPI();
     patient myECPatient2 = new patient();
     doctor myECdoctor2 = new doctor();
==================================================================

A little info on best practice. Functions and subroutine should do one thing only. For example the  UpdateTable should only do that, update the table with the passed in record. Do not create new records in this function it will create problems once the software has been released and needs maintenance or new features added.  

Fernando
private static void UpdateTable(DataClasses1DataContext db, Object record)
{
    // Get the type of the record to insert
    string type = record.GetType().Name;
 
 
    // Then one if statement for each table type
    // This is where you tell the DataContext on what table to do the insert on
    // with the record that was passed into this function with the Datacontext 
    // that was also passed in.
    if (type == "MPI")
        db.MPIs.InsertOnSubmit(record as MPI);
 
    if (type == "PATIENT")
        db.PATIENTs.InsertOnSubmit(record as PATIENT);
 
    if (type == "EMPLOYER")
        db.EMPLOYERs.InsertOnSubmit(record as EMPLOYER);
 
    if (type == "PATIENT_PHONE_NUMBER")
        db.PATIENT_PHONE_NUMBERs.InsertOnSubmit(record as PATIENT_PHONE_NUMBER);
 
    if (type == "Patient_Address")
        db.Patient_Addresses.InsertOnSubmit(record as Patient_Address);
 
    if (type == "PATIENTS_ATTENDED_BY_DOCTOR")
        db.PATIENTS_ATTENDED_BY_DOCTORs.InsertOnSubmit(record as PATIENTS_ATTENDED_BY_DOCTOR);
 
    if (type == "PATIENT_IN")
        db.PATIENT_INs.InsertOnSubmit(record as PATIENT_IN);
 
    if (type == "BillingAlert")
        db.BillingAlerts.InsertOnSubmit(record as BillingAlert);
 
    if (type == "EligibilityStatus")
        db.EligibilityStatus.InsertOnSubmit(record as EligibilityStatus);
 
 
 
    // ---------------  Write Record -------------------------
    // This single try/catch statement will insert the recors that was flaged to be
    // inserted in the above if statement.
 
    try
    {
        // Attempt to update the database with the record flagged with the if statement above
        db.SubmitChanges();
    }
    catch (DuplicateKeyException dke)
    {
        // This DuplicateKeyException is a Linq exception for duplicate keys that exist
        // in the DataContect and not the database, reason why it was not catching it.
        // Remove record from DataContext and continue.
        // ********* ONE CASE STATEMENT FOR EACH TABLE TYPE ***************
        switch (type)
        {
            case "MPI":
                db.MPIs.DeleteOnSubmit(record as MPI);
                break;
            case "PATIENT_PHONE_NUMBER":
                db.PATIENT_PHONE_NUMBERs.DeleteOnSubmit(record as PATIENT_PHONE_NUMBER);
                break;
            case "Patient_Address":
                db.Patient_Addresses.DeleteOnSubmit(record as Patient_Address);
                break;
            case "PATIENTS_ATTENDED_BY_DOCTOR":
                db.PATIENTS_ATTENDED_BY_DOCTORs.DeleteOnSubmit(record as PATIENTS_ATTENDED_BY_DOCTOR);
                break;
            // More case statements here for the rest of the table types
        }
    }
    catch (SqlException sqle)
    {
        // This is a duplicate record found in the databas, remove record from DataContext.
        // ********* ONE CASE STATEMENT FOR EACH TABLE TYPE ***************
        switch (type)
        {
            case "MPI":
                db.MPIs.DeleteOnSubmit(record as MPI);
                break;
            case "PATIENT_PHONE_NUMBER":
                db.PATIENT_PHONE_NUMBERs.DeleteOnSubmit(record as PATIENT_PHONE_NUMBER);
                break;
            case "Patient_Address":
                db.Patient_Addresses.DeleteOnSubmit(record as Patient_Address);
                break;
            case "PATIENTS_ATTENDED_BY_DOCTOR":
                db.PATIENTS_ATTENDED_BY_DOCTORs.DeleteOnSubmit(record as PATIENTS_ATTENDED_BY_DOCTOR);
                break;
            // More case statements here for the rest of the table types
        }
    }
}

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now