1
votes

How Come this is not allowed?

public void addInsertedInformationToDb() { 

            using(DbClassesDataContext myDb = new DbClassesDataContext(dbPath)){

                myDb.PatientInfos.InsertOnSubmit(insertPersonalInformation);
            }
        }

Where my insertPersonalInformation

 private PatientInfo insertPersonalInformation()
        {

            DbClassesDataContext myDb = new DbClassesDataContext(dbPath);

            PatientInfo patientInfo = new PatientInfo();

            patientInfo.Phy_ID = physcianID;
            patientInfo.Pat_First_Name = txtFirstName.Text;
            patientInfo.Pat_Middle_Name = txtMiddleName.Text;
            patientInfo.Pat_Last_Name = txtLastName.Text;
            patientInfo.Pat_Gender = cmbGender.Text;
            patientInfo.Pat_Marital_Status = cmbMaritalStatus.Text;
            patientInfo.Pat_Date_Of_Birth = dtpDOB.Value;
            patientInfo.Pat_Home_Add = txtHomeAdd.Text;
            patientInfo.Pat_Home_Num = txtPhone.Text;
            patientInfo.Pat_Work_Add = txtWorkAdd.Text;
            patientInfo.Pat_Work_Num = txtWorkPhone.Text;
            patientInfo.Pat_Prim_Physician = txtPrimPhysician.Text;
            patientInfo.Pat_Ref_Physician = txtRefePhysician.Text;

            return patientInfo;
        }

I kept on getting these errors

Error 1 The best overloaded method match for 'System.Data.Linq.Table.InsertOnSubmit(PatientAdministration.PatientInfo)' has some invalid arguments C:\Users\John\documents\visual studio 2010\Projects\PatientAdministration\PatientAdministration\Pat_Demog.cs 101 17 PatientAdministration

Error 2 Argument 1: cannot convert from 'method group' to 'PatientAdministration.PatientInfo' C:\Users\John\documents\visual studio 2010\Projects\PatientAdministration\PatientAdministration\Pat_Demog.cs 101 50 PatientAdministration

What is the fix for this?

2

2 Answers

1
votes

Because in InsertOnSubmit you can just use entity object or valid expressions not calling arbitrary methods, linq2entity can't convert your method to required expression, You can do as below to come up with your problem:

       using(DbClassesDataContext myDb = new DbClassesDataContext(dbPath)){
            var patientInfo = insertPersonalInformation();
            myDb.PatientInfos.InsertOnSubmit(patientInfo);
        }

See Insert with linq2entity:

To execute a SQL Insert, just add objects to the object model you have created, and call SubmitChanges on the DataContext.

Finally don't forgot to call myDb.SubmitChanges().

0
votes

Your code works just fine just call the method like this:

myDb.PatientInfos.InsertOnSubmit(insertPersonalInformation());
myDb.SubmitChanges()

You forgot to add the parentheses.