Exemplo n.º 1
0
 public void CustomerFormView_InsertItem(Customer customer)
 {
     if (ModelState.IsValid)
     {
         try
         {
             Service.SaveContact(customer);
             Session["succes"] = "Användaren sparades";
             Response.Redirect("/Pages/AddCustomer.aspx");
         }
         catch (Exception)
         {
             ModelState.AddModelError(String.Empty, "oväntat fel när kunden skulle Läggas till.");
         }
     }
 }
Exemplo n.º 2
0
        public void SaveContact(Customer customer)
        {
            // Validera affärsreglerna
            var validationContext = new ValidationContext(customer);
            var validationResults = new List<ValidationResult>();
            if (!Validator.TryValidateObject(customer, validationContext, validationResults, true))
            {
                var ex = new ValidationException("Kunden kunde inte sparas.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if (customer.CustomerID == 0)
            {
                CustomerDAL.InsertCustomer(customer);
            }
            else
            {
                CustomerDAL.UpdateCustomer(customer);
            }
        }
Exemplo n.º 3
0
        // Redigera en kund
        public void UpdateCustomer(Customer Customer)
        {
            using (var conn = CreateConnection())
            {
                try
                {
                    var cmd = new SqlCommand("dbo.usp_UpdateCustomer", conn);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@CustomerID", SqlDbType.Int, 4).Value = Customer.CustomerID;
                    cmd.Parameters.Add("@CustomerNUM", SqlDbType.Int, 4).Value = Customer.CustomerNUM;
                    cmd.Parameters.Add("@Name", SqlDbType.VarChar, 25).Value = Customer.Name;
                    cmd.Parameters.Add("@City", SqlDbType.VarChar, 25).Value = Customer.City;
                    cmd.Parameters.Add("@PostalCode", SqlDbType.VarChar, 6).Value = Customer.PostalCode;
                    cmd.Parameters.Add("@Phone", SqlDbType.VarChar, 15).Value = Customer.Phone;

                    conn.Open();

                    cmd.ExecuteNonQuery();

                }
                catch
                {
                    throw new ArgumentException("Fel vid anslutning till databasen");
                }

            }
        }