/// <summary>
        /// Updates a Firearm in the Database.
        /// Returns the updated Firearm.
        /// </summary>
        /// <param name="firearm">The Firearm to update.</param>
        /// <returns></returns>
        public static Firearms UpdateFirearm(Firearms firearm)
        {
            using (var context = new SimpleGunShopDatabaseEntities())
            {
                try
                {
                    context.Firearms.Add(firearm);
                    context.Entry(firearm).State = EntityState.Modified; // Tell EF it has been modified
                    context.SaveChanges();
                    return(firearm);
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);

                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
            }
        }
        private void DisplayUpdateForm()
        {
            Firearms selectedFirearm = lstFirearms.SelectedItem as Firearms;
            var      updateForm      = new FormUpdateFirearm(selectedFirearm);

            updateForm.ShowDialog();
        }
 /// <summary>
 /// Deletes a Firearm from the Database.
 /// </summary>
 /// <param name="firearm"></param>
 public static void DeleteFirearm(Firearms firearm)
 {
     using (var context = new SimpleGunShopDatabaseEntities())
     {
         context.Firearms.Add(firearm);                      // Track with EF
         context.Entry(firearm).State = EntityState.Deleted; // Tell EF we are removing it
         context.SaveChanges();                              // Update on DB
     }
 }
 /// <summary>
 /// Adds a Firearm. Returns the newly added Firearm
 /// with the FirearmID populated
 /// </summary>
 /// <param name="firearm">The Firearm to add.</param>
 /// <returns></returns>
 public static Firearms AddFirearm(Firearms firearm)
 {
     using (var context = new SimpleGunShopDatabaseEntities())
     {
         context.Firearms.Add(firearm);
         context.SaveChanges();
         context.Entry(firearm).State = EntityState.Added; // Tell EF firearm has been added
         return(firearm);
     }
 }
 public FormUpdateFirearm(Firearms f = null)
 {
     InitializeComponent();
     existingFirearm = f;
     if (f != null)
     {
         Text = "Updating firearm : " + f.SerialNumber;
         //Populate existing firearm on the update form with main form
         txtUpdateSerialNum.Text    = existingFirearm.SerialNumber;
         txtUpdateManufacturer.Text = existingFirearm.Manufacturer;
         txtUpdateModel.Text        = existingFirearm.ModelName;
         txtUpdateCaliber.Text      = existingFirearm.Caliber;
         txtUpdatePrice.Text        = Convert.ToString(existingFirearm.Price);
     }
 }
        private void btnUpdateFirearm_Click(object sender, EventArgs e)
        {
            Firearms gun = new Firearms();

            try
            {
                //Keep the ID of the gun the same because we are UPDATING
                gun.FirearmID = existingFirearm.FirearmID;
                FirearmDB.UpdateFirearm(gun);
                MessageBox.Show("Firearm has been updated!");
                Close();
            }
            catch (SqlException ex)
            {
                MessageBox.Show("There was an issue contacting the Database, please try agian");
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.GetType().ToString());
            }
        }
Beispiel #7
0
        private void btnAddFirearm_Click(object sender, EventArgs e)
        {
            Firearms f = new Firearms()
            {
                SerialNumber = txtSerialNumber.Text,
                Manufacturer = txtManufacturer.Text,
                ModelName    = txtModel.Text,
                Caliber      = txtCaliper.Text,
                Price        = Convert.ToDecimal(txtPrice.Text)
            };

            try
            {
                FirearmDB.AddFirearm(f);
                MessageBox.Show("Firearm was added!");
                Close();
            }
            catch (SqlException)
            {
                MessageBox.Show("Database Error", "There was an issue when trying to add to the DB");
            }
        }