# Inserting Data in The Database using LINQ TO SQL

Can this Code be any shorter? because it seems it is very long with just simple insertion of data using LINQ TO SQL

 public void addPatientInformation() {

using(DbClassesDataContext myDb = new DbClassesDataContext()){

PatientInfo patientInfo = new PatientInfo();

patientInfo.Phy_ID = physcianID;
patientInfo.Pat_First_Name = txtFirstName.Text;
patientInfo.Pat_Middle_Init = 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_Num = txtPhone.Text;
patientInfo.Pat_Work_Num = txtWorkPhone.Text;
patientInfo.Pat_Prim_Physician = txtPrimPhysician.Text;
patientInfo.Pat_Ref_Physician = txtRefePhysician.Text;

myDb.PatientInfos.InsertOnSubmit(patientInfo);
myDb.SubmitChanges();
}


Yes. You can use object initializer syntax and bring the amount of text down a bit:

     public void addPatientInformation() {
using(DbClassesDataContext myDb = new DbClassesDataContext()) {
myDb.PatientInfos.InsertOnSubmit(new PatientInfo {
Phy_ID = physcianID,
Pat_First_Name = txtFirstName.Text,
Pat_Middle_Init = txtMiddleName.Text,
Pat_Last_Name = txtLastName.Text,
Pat_Gender = cmbGender.Text,
Pat_Marital_Status = cmbMaritalStatus.Text,
Pat_Date_Of_Birth = dtpDOB.Value,
Pat_Home_Num = txtPhone.Text,
Pat_Work_Num = txtWorkPhone.Text,
Pat_Prim_Physician = txtPrimPhysician.Text,
Pat_Ref_Physician = txtRefePhysician.Text,
});
myDb.SubmitChanges();
}
}

• Alternately, create a constructor for the PatientInfo class that takes all of those parameters and assigns them appropriately. Then you'll trim a bit more out of this call. – Jesse C. Slicer Mar 2 '12 at 15:38
• No magic here, this is one of those simple, dumb and tedious code is the right code situations. I would however maybe put the form with all the controls into a custom control with a method you can call on it that will do this mapping and spit out the Patient object for you since the mapping is coupled to that particular group of controls. – Jimmy Hoffa Mar 3 '12 at 6:18

In short. No. The mapping between object properties and fields on the form has to be somewhere.

What you could do though is to set it up by convention. So if the field on the form had the same name as the corresponding property on the object you could use reflection (or a tool like Automapper) to do that shifting. In pseudo code (because I don't have a set up .Net environment in front of me right now) it would be something like:

public static void MapControlValuesToFields<T, F>(T obj, F form)
where T : class
where F : Form {  //or whatever your base class for forms is
get all fields of typeof(F)
where type of the field inherits from Control
where typeof(T) has a similarly named setter property
get the value of each in form (the .Text of .Value logic would go here)
for each set the value on object
}


and then you would be able to do something like

  var patient = new PatientInfo();
MapControlValuesToFields(patient, form);
//at this point the values are in the patient and you can save it


This is of course fairly advanced coding but it is possible and probably only about 10 lines of code. Like I said you can look at Automapper for some of this functionality out of the box (though I don't know how well it would do with windows/web forms).

I will say that what you're looking for is similar to the functionality provided by the MVVM pattern so if you're doing windows forms you can look at Truss. If you're doing web forms...well I wouldn't do web forms, this functionality is already in ASP MVC and silverlight which are both like a million times easier to work with but I remember there being some buzz about the ASP MVP project that would help with those issues.