Reputation: 16147
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?
Upvotes: 1
Views: 1457
Reputation: 12786
Your code works just fine just call the method like this:
myDb.PatientInfos.InsertOnSubmit(insertPersonalInformation());
myDb.SubmitChanges()
You forgot to add the parentheses.
Upvotes: 0
Reputation: 22565
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);
}
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()
.
Upvotes: 1