Пример #1
0
 public void SaveContact(Contact contact)
 {
     ICollection<ValidationResult> validationResults;
     if (!contact.Validate(out validationResults))
     {
         throw new AggregateException("Objektet klarade inte valideringen.",
             validationResults.Select(vr => new ValidationException(vr.ErrorMessage)).ToList().AsReadOnly());
     }
     
     if (contact.ContactId == 0)
     {
         ContactDAL.InsertContact(contact);
     }
     else
     {
         ContactDAL.UpdateContact(contact);
     }
 }
Пример #2
0
 public void ContactFormView_InsertItem(Contact contact)
 {
     //ModelState tittar på statusen beträffande DataAnnotations
     //kontra IsValid som kollar valideringskontroller i aspx-filen
     if (ModelState.IsValid)
     {
         try
         {
             Service.SaveContact(contact);
             Response.RedirectToRoute("Default");
             Session["Success"] = true;
             InsertPlaceHolder.Visible = true;
             SuccessPlaceHolder.Visible = true;
         }
         catch (Exception)
         {
             ModelState.AddModelError(String.Empty, "Ett fel inträffade då kontakten skulle läggas till.");
         }
     }
 }
Пример #3
0
        public void InsertContact(Contact contact)
        {
            using (var conn = CreateConnection()) 
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("Person.uspAddContact", conn);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 30).Value = contact.FirstName;
                    cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 30).Value = contact.LastName;
                    cmd.Parameters.Add("@EmailAddress", SqlDbType.VarChar, 40).Value = contact.EmailAddress;

                    cmd.Parameters.Add("@ContactId", SqlDbType.Int, 4).Value = ParameterDirection.Output;

                    conn.Open();

                    cmd.ExecuteNonQuery();

                    contact.ContactId = (int)cmd.Parameters["@ContactId"].Value;
                }
                catch (Exception)
                {
                    throw new ApplicationException("Ett fel inträffade när kontakten skulle skapas.");
                }

            }
        }