public static void SaveContact(Contact contact)
        {
            if (!contact.Validate()) {
                throw new ValidationException();
            }

            if (contact.ContactID == 0) {
                ContactDAL.InsertContact(contact);
            } else {
                ContactDAL.UpdateContact(contact);
            }
        }
 public void ListView_InsertItem()
 {
     var contact = new Contact();
     TryUpdateModel(contact);
     if (ModelState.IsValid) {
         try {
             Service.SaveContact(contact);
             Session["message"] = "Kontakten lades till";
             Response.Redirect("~/Default.aspx");
         } catch {
             ModelState.AddModelError(String.Empty, "Ett fel inträffade när kontakten skapades");
         }
     }
 }
        // INSERT & UPDATE
        public void SaveContact(Contact contact)
        {
            var validationContext = new ValidationContext(contact);
            var validationResults = new List<ValidationResult>();

            // Check if data passes objectvalidation
            if (!Validator.TryValidateObject(contact, validationContext, validationResults, true))
            {
                var ex = new ValidationException("Inmatade värden uppfyllder ej valideringskraven");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if(contact.ContactID == 0)
                ContactDAL.InsertContact(contact);
            else
                ContactDAL.UpdateContact(contact);
        }
        public void FormView_InsertItem(Contact contact)
        {
            // Check Validationcontrols on server
            if(IsValid)
            {
                // Check if data passes objectvalidation
                if (ModelState.IsValid)
                {
                    try
                    {
                        Service.SaveContact(contact);
                        Session["Post"] = true;

                        // Redirect to prevent dubbelposting
                        Response.Redirect("~/");
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(String.Empty, ex.Message);
                    }
                }
            }
        }
 public static void DeleteContact(Contact contact)
 {
     DeleteContact(contact.ContactID);
 }