public void UpdateMedication(int Id) { using (var db = new DiabetesRegistryEntities1()) { PassportMedication med = db.PassportMedications.Find(Id); if (med == null) { ModelState.AddModelError("Error", String.Format("Item with id {0} was not found", Id)); return; } TryUpdateModel(med); if (ModelState.IsValid) { try { db.SaveChanges(); lsvMedication.EditIndex = -1; ErrorLabel.Text = String.Empty; } catch (Exception exp) { ErrorLabel.Text = exp.Message; } } } }
public void SaveMedication() { ClearErrorLabels(); using (var db = new DiabetesRegistryEntities1()) { var med = new PassportMedication(); //todo add code for getting the item added TryUpdateModel(med); if (ModelState.IsValid) { var regId = int.Parse(Request.QueryString["regId"]); var regType = int.Parse(Request.QueryString["regType"]); try { if (recordId.Value == "0")//meaning no passport saved { var pass = new DiabetesPassport() { RegistrationId = regId, RegistrationType = regType }; db.DiabetesPassports.Add(pass); db.SaveChanges(); ViewState["passId"] = pass.Id; recordId.Value = pass.Id.ToString(); } med.PassportId = int.Parse(recordId.Value); db.PassportMedications.Add(med); db.SaveChanges(); //NOT yet since we don't have the ID of the record // List<PassportMedication> meds = new List<PassportMedication>(); //if (ViewState["newMeds"] != null) //{ // meds = (List<PassportMedication>)ViewState["newMeds"]; //} //meds.Add(med); //ViewState["newMeds"] = meds; ErrorLabel.Text = String.Empty; } catch (Exception exp) { ErrorLabel.Text = exp.Message; } } else { //TODO add code for showing model errors // lblErrorsSection6.Text = "Section contains errors"; } } }