private void dgv_ammunition_DoubleClick(object sender, EventArgs e) { btn_save.Enabled = true; if (dgv_gear.CurrentRow.Index != -1) { Gear gear = new Gear(); currentID = Convert.ToInt32(dgv_gear.CurrentRow.Cells["col_gearId"].Value); using (EFTDBEntities context = new EFTDBEntities()) { gear = context.Gears.Where(g => g.GearID == currentID).FirstOrDefault(); txt_name.Text = gear.Name; cbox_type.SelectedIndex = cbox_type.FindStringExact(gear.Type); cbox_slot.SelectedIndex = cbox_slot.FindStringExact(gear.Slot); txt_weight.Text = gear.Weight.ToString(); txt_material.Text = gear.Material; cbox_class.SelectedIndex = cbox_class.FindStringExact(gear.Class.ToString()); txt_durability.Text = gear.Durability.ToString(); txt_slots.Text = gear.Slots.ToString(); txt_movementSpeed.Text = gear.MovementSpeed.ToString(); txt_turnSpeed.Text = gear.TurnSpeed.ToString(); txt_ergonomics.Text = gear.Ergonomics.ToString(); txt_ricochetChance.Text = gear.RicochetChance.ToString(); cbox_soundReduction.SelectedIndex = cbox_soundReduction.FindStringExact(gear.SoundReduction); chk_blocksEarpiece.Checked = (bool)gear.BlocksEarpiece; chk_blocksEyewear.Checked = (bool)gear.BlocksEyewear; chk_blocksFaceCover.Checked = (bool)gear.BlocksFaceCover; chk_blocksHeadwear.Checked = (bool)gear.BlocksHeadwear; chk_blocksArmour.Checked = (bool)gear.BlocksArmour; } btn_delete.Enabled = true; btn_add.Enabled = false; } }
// Perform some validation on the fields then send an UPDATE query to the database with Entity Framework. // Send a message to the user with the result. private void btn_resetPassword_Click(object sender, EventArgs e) { if (txt_password.Text == "" || txt_passwordRepeat.Text == "") { lbl_errorMessage.Text = "Password cannot be empty."; panel_error.Visible = true; return; } else if (txt_password.Text != txt_passwordRepeat.Text) { lbl_errorMessage.Text = "Passwords do not match."; panel_error.Visible = true; return; } using (EFTDBEntities context = new EFTDBEntities()) { User user = new User() { UserID = currentUser.UserID, Username = currentUser.Username, Password = txt_password.Text, Email = currentUser.Email, Admin = currentUser.Admin }; context.Entry(user).State = EntityState.Modified; context.SaveChanges(); } mainMenu.Logout("Password changed successfully, please log in."); }
// If the data is valid, create a new instance of the appropriate entity and input the data. Change the entity state to Modified in the context // When the changes are saved the database will update the row using a standard UPDATE query. private void btn_save_Click(object sender, EventArgs e) { if (ValidateData()) { using (EFTDBEntities context = new EFTDBEntities()) { Loadout loadout = new Loadout() { LoadoutID = currentID, Name = txt_name.Text, UserID = currentUser.UserID, EarpieceID = context.Gears.Where(earpiece => earpiece.Name == cbox_earpiece.Text).FirstOrDefault().GearID, HeadwearID = context.Gears.Where(headwear => headwear.Name == cbox_headwear.Text).FirstOrDefault().GearID, BodyArmourID = context.Gears.Where(bodyArmour => bodyArmour.Name == cbox_bodyArmour.Text).FirstOrDefault().GearID, PrimaryWeaponID = context.Weapons.Where(primaryWeapon => primaryWeapon.Name == cbox_primaryWeapon.Text).FirstOrDefault().WeaponID, SecondaryWeaponID = context.Weapons.Where(secondaryWeapon => secondaryWeapon.Name == cbox_secondaryWeapon.Text).FirstOrDefault().WeaponID, HolsterWeaponID = context.Weapons.Where(holsterWeapon => holsterWeapon.Name == cbox_holsterWeapon.Text).FirstOrDefault().WeaponID, RigID = context.Gears.Where(rig => rig.Name == cbox_rig.Text).FirstOrDefault().GearID, BackpackID = context.Gears.Where(backpack => backpack.Name == cbox_backpack.Text).FirstOrDefault().GearID, }; context.Entry(loadout).State = EntityState.Modified; context.SaveChanges(); LoadData(); MessageBox.Show($"Updated {loadout.Name} successfully!"); } Clear(); btn_save.Enabled = false; btn_add.Enabled = true; btn_delete.Enabled = false; } }
// When a row in the datagridview is clicked, check to make sure it's not the header then store the row's itemID in the currentID variable // Query the database to find the appropriate item and populate the fields with it's data. In the case of comboboxes match the Index with the name. private void dgv_weapons_DoubleClick(object sender, EventArgs e) { btn_save.Enabled = true; if (dgv_loadouts.CurrentRow.Index != -1) { Loadout loadout = new Loadout(); currentID = Convert.ToInt32(dgv_loadouts.CurrentRow.Cells["col_loadoutID"].Value); using (EFTDBEntities context = new EFTDBEntities()) { loadout = context.Loadouts.Where(l => l.LoadoutID == currentID).FirstOrDefault(); txt_name.Text = loadout.Name; // Search the database for the row with the corresponding ItemID, find the combobox index which matches the name of the Item. cbox_earpiece.SelectedIndex = cbox_earpiece.FindStringExact(context.Gears.Where(earpiece => earpiece.GearID == loadout.EarpieceID).FirstOrDefault().Name); cbox_headwear.SelectedIndex = cbox_headwear.FindStringExact(context.Gears.Where(headwear => headwear.GearID == loadout.HeadwearID).FirstOrDefault().Name); cbox_bodyArmour.SelectedIndex = cbox_bodyArmour.FindStringExact(context.Gears.Where(bodyArmour => bodyArmour.GearID == loadout.BodyArmourID).FirstOrDefault().Name); cbox_primaryWeapon.SelectedIndex = cbox_primaryWeapon.FindStringExact(context.Weapons.Where(primaryWeapon => primaryWeapon.WeaponID == loadout.PrimaryWeaponID).FirstOrDefault().Name); cbox_secondaryWeapon.SelectedIndex = cbox_secondaryWeapon.FindStringExact(context.Weapons.Where(secondaryWeapon => secondaryWeapon.WeaponID == loadout.SecondaryWeaponID).FirstOrDefault().Name); cbox_holsterWeapon.SelectedIndex = cbox_holsterWeapon.FindStringExact(context.Weapons.Where(holsterWeapon => holsterWeapon.WeaponID == loadout.HolsterWeaponID).FirstOrDefault().Name); cbox_rig.SelectedIndex = cbox_rig.FindStringExact(context.Gears.Where(rig => rig.GearID == loadout.RigID).FirstOrDefault().Name); cbox_backpack.SelectedIndex = cbox_backpack.FindStringExact(context.Gears.Where(backpack => backpack.GearID == loadout.BackpackID).FirstOrDefault().Name); } btn_delete.Enabled = true; btn_add.Enabled = false; } }
// If the data is valid create a new instance of the DBContext and add the entity. // When the changes are saved the database will insert the row using a standard INSERT query. private void btn_add_Click(object sender, EventArgs e) { if (ValidateData()) { using (EFTDBEntities context = new EFTDBEntities()) { Loadout loadout = new Loadout() { Name = txt_name.Text, UserID = currentUser.UserID, // Get the ID of the appropriate gear using a lambda expression LINQ to Entity query. EarpieceID = context.Gears.Where(earpiece => earpiece.Name == cbox_earpiece.Text).FirstOrDefault().GearID, HeadwearID = context.Gears.Where(headwear => headwear.Name == cbox_headwear.Text).FirstOrDefault().GearID, BodyArmourID = context.Gears.Where(bodyArmour => bodyArmour.Name == cbox_bodyArmour.Text).FirstOrDefault().GearID, PrimaryWeaponID = context.Weapons.Where(primaryWeapon => primaryWeapon.Name == cbox_primaryWeapon.Text).FirstOrDefault().WeaponID, SecondaryWeaponID = context.Weapons.Where(secondaryWeapon => secondaryWeapon.Name == cbox_secondaryWeapon.Text).FirstOrDefault().WeaponID, HolsterWeaponID = context.Weapons.Where(holsterWeapon => holsterWeapon.Name == cbox_holsterWeapon.Text).FirstOrDefault().WeaponID, RigID = context.Gears.Where(rig => rig.Name == cbox_rig.Text).FirstOrDefault().GearID, BackpackID = context.Gears.Where(backpack => backpack.Name == cbox_backpack.Text).FirstOrDefault().GearID }; context.Loadouts.Add(loadout); context.SaveChanges(); LoadData(); MessageBox.Show($"Loadout '{loadout.Name}' added successfully!"); } } }
// Check if the user is sure about deleting, then get the itemID from the currentID and change the entity state to Deleted in the context. // When the changes are saved the database will delete the row using a standard DELETE query. private void btn_delete_Click(object sender, EventArgs e) { if (MessageBox.Show($"Are you sure you want to Delete {txt_name.Text}?", "SQL deletion operation", MessageBoxButtons.YesNo) == DialogResult.Yes) { Loadout loadout = new Loadout(); using (EFTDBEntities context = new EFTDBEntities()) { loadout = context.Loadouts.Where(l => l.LoadoutID == currentID).FirstOrDefault(); var entry = context.Entry(loadout); // Make sure the entity is being tracked by the context. if (entry.State == EntityState.Detached) { context.Loadouts.Attach(loadout); } // Change the entities state to deleted. context.Loadouts.Remove(loadout); // Save changes made to the context (removing the deleted entity) to the database. context.SaveChanges(); // Reload the datasources. LoadData(); } Clear(); } // Enable/Disable appropriate buttons. btn_delete.Enabled = false; btn_save.Enabled = false; btn_add.Enabled = true; }
// When a row in the datagridview is clicked, check to make sure it's not the header then store the row's itemID in the currentID variable // Query the database to find the appropriate item and populate the fields with it's data. In the case of comboboxes match the Index with the name. private void dgv_ammunition_DoubleClick(object sender, EventArgs e) { btn_save.Enabled = true; if (dgv_ammunition.CurrentRow.Index != -1) { Ammunition ammunition = new Ammunition(); currentID = Convert.ToInt32(dgv_ammunition.CurrentRow.Cells["col_ammunitionID"].Value); using (EFTDBEntities context = new EFTDBEntities()) { ammunition = context.Ammunitions.Where(a => a.AmmunitionID == currentID).FirstOrDefault(); string caliberName = context.Calibers.Where(c => c.CaliberID == ammunition.CaliberID).FirstOrDefault().Name; txt_name.Text = ammunition.Name; txt_damage.Text = ammunition.Damage.ToString(); txt_penetration.Text = ammunition.Penetration.ToString(); txt_armourDamage.Text = ammunition.ArmourDamage.ToString(); txt_accuracy.Text = ammunition.Accuracy.ToString(); txt_recoil.Text = ammunition.Recoil.ToString(); txt_fragmentationChance.Text = ammunition.FragmentationChance.ToString(); txt_ricochetChance.Text = ammunition.RicochetChance.ToString(); txt_speed.Text = ammunition.Speed.ToString(); cbox_caliberID.SelectedIndex = cbox_caliberID.FindStringExact(caliberName); } btn_delete.Enabled = true; btn_add.Enabled = false; } }
// Check if the user is already in the database and if the passwords match. Show errors if appropriate. private void btn_login_Click(object sender, EventArgs e) { using (EFTDBEntities context = new EFTDBEntities()) { User user = context.Users.Where(u => u.Username == txt_username.Text).FirstOrDefault <User>(); if (user == null) { lbl_errorMessage.Text = "User not found."; panel_error.Visible = true; return; } else if (txt_password.Text != user.Password) { lbl_errorMessage.Text = "Incorrect username or password."; panel_error.Visible = true; return; } else { // Call the login function on the main menu form. mainMenu.Login(user); } } }
// If the data is valid, create a new instance of the appropriate entity and input the data. Change the entity state to Modified in the context // When the changes are saved the database will update the row using a standard UPDATE query. private void btn_save_Click(object sender, EventArgs e) { if (ValidateData()) { using (EFTDBEntities context = new EFTDBEntities()) { int caliberID = context.Calibers.Where(c => c.Name == cbox_caliberID.Text).FirstOrDefault().CaliberID; Ammunition ammunition = new Ammunition() { AmmunitionID = currentID, Name = txt_name.Text, Damage = int.Parse(txt_damage.Text), Penetration = int.Parse(txt_penetration.Text), ArmourDamage = int.Parse(txt_armourDamage.Text), Accuracy = int.Parse(txt_accuracy.Text), Recoil = int.Parse(txt_recoil.Text), FragmentationChance = decimal.Parse(txt_fragmentationChance.Text), RicochetChance = decimal.Parse(txt_ricochetChance.Text), Speed = int.Parse(txt_speed.Text), CaliberID = caliberID }; context.Entry(ammunition).State = EntityState.Modified; context.SaveChanges(); LoadData(); MessageBox.Show($"Updated {ammunition.Name} successfully!"); } Clear(); btn_save.Enabled = false; btn_add.Enabled = true; btn_delete.Enabled = false; } }
// If the data is valid create a new instance of the DBContext and add the entity. // When the changes are saved the database will insert the row using a standard INSERT query. private void btn_add_Click(object sender, EventArgs e) { if (ValidateData()) { using (EFTDBEntities context = new EFTDBEntities()) { // Fetch the corresponding caliberID from the database by using a LINQ to Entity query, returning the first row or null. int caliberID = context.Calibers.Where(c => c.Name == cbox_caliberID.Text).FirstOrDefault().CaliberID; Ammunition ammunition = new Ammunition() { Name = txt_name.Text, Damage = int.Parse(txt_damage.Text), Penetration = int.Parse(txt_penetration.Text), ArmourDamage = int.Parse(txt_armourDamage.Text), Accuracy = int.Parse(txt_accuracy.Text), Recoil = int.Parse(txt_recoil.Text), FragmentationChance = decimal.Parse(txt_fragmentationChance.Text), RicochetChance = decimal.Parse(txt_ricochetChance.Text), Speed = int.Parse(txt_speed.Text), CaliberID = caliberID }; // Add the new entity to the context with the Added state. context.Ammunitions.Add(ammunition); // Save changes made to the context to the database. context.SaveChanges(); // Reload the datasources. LoadData(); MessageBox.Show($"Added {ammunition.Name} successfully!"); } Clear(); } }
// When a row in the datagridview is clicked, check to make sure it's not the header then store the row's itemID in the currentID variable // Query the database to find the appropriate item and populate the fields with it's data. In the case of comboboxes match the Index with the name. private void dgv_weapons_DoubleClick(object sender, EventArgs e) { btn_save.Enabled = true; if (dgv_weapons.CurrentRow.Index != -1) { Weapon weapon = new Weapon(); currentID = Convert.ToInt32(dgv_weapons.CurrentRow.Cells["col_weaponID"].Value); using (EFTDBEntities context = new EFTDBEntities()) { weapon = context.Weapons.Where(w => w.WeaponID == currentID).FirstOrDefault(); string caliberName = context.Calibers.Where(c => c.CaliberID == weapon.CaliberID).FirstOrDefault().Name; txt_name.Text = weapon.Name; cbox_type.SelectedIndex = cbox_type.FindStringExact(weapon.Type); cbox_slot.SelectedIndex = cbox_slot.FindStringExact(weapon.Slot); txt_weight.Text = weapon.Weight.ToString(); txt_verticalRecoil.Text = weapon.VerticalRecoil.ToString(); txt_horizontalRecoil.Text = weapon.HorizontalRecoil.ToString(); txt_range.Text = weapon.Range.ToString(); txt_ergonomics.Text = weapon.Ergonomics.ToString(); txt_fireRate.Text = weapon.FireRate.ToString(); chk_single.Checked = (bool)weapon.Single; chk_burst.Checked = (bool)weapon.Burst; chk_auto.Checked = (bool)weapon.Auto; cbox_caliberID.SelectedIndex = cbox_caliberID.FindStringExact(caliberName); } btn_delete.Enabled = true; btn_add.Enabled = false; } }
// If the data is valid, create a new instance of the appropriate entity and input the data. Change the entity state to Modified in the context // When the changes are saved the database will update the row using a standard UPDATE query. private void btn_save_Click(object sender, EventArgs e) { if (ValidateData()) { using (EFTDBEntities context = new EFTDBEntities()) { User currentUser = context.Users.AsNoTracking().Where(cu => cu.UserID == currentID).FirstOrDefault(); User user = new User() { UserID = currentID, Username = txt_username.Text, Password = currentUser.Password, Email = txt_email.Text, Admin = chk_admin.Checked }; context.Entry(user).State = EntityState.Modified; context.SaveChanges(); LoadData(); MessageBox.Show($"Updated {user.Username} successfully!"); } Clear(); btn_save.Enabled = false; btn_delete.Enabled = false; } }
// If the data is valid create a new instance of the DBContext and add the entity. // When the changes are saved the database will insert the row using a standard INSERT query. private void btn_add_Click(object sender, EventArgs e) { if (ValidateData()) { using (EFTDBEntities context = new EFTDBEntities()) { int caliberID = context.Calibers.Where(c => c.Name == cbox_caliberID.Text).FirstOrDefault().CaliberID; Weapon weapon = new Weapon() { Name = txt_name.Text, Type = cbox_type.Text, Slot = cbox_slot.Text, Weight = decimal.Parse(txt_weight.Text), VerticalRecoil = int.Parse(txt_verticalRecoil.Text), HorizontalRecoil = int.Parse(txt_horizontalRecoil.Text), Range = int.Parse(txt_range.Text), Ergonomics = int.Parse(txt_ergonomics.Text), FireRate = int.Parse(txt_fireRate.Text), Single = chk_single.Checked, Burst = chk_burst.Checked, Auto = chk_auto.Checked, CaliberID = caliberID }; // Add the new entity to the context with the Added state. context.Weapons.Add(weapon); // Save changes made to the context to the database. context.SaveChanges(); // Reload the datasources. LoadData(); MessageBox.Show($"Added {weapon.Name} successfully!"); } Clear(); } }
private void LoadData() { using (EFTDBEntities context = new EFTDBEntities()) { context.Gears.Load(); gearBindingSource.DataSource = context.Gears.Local.ToBindingList(); dgv_gear.Columns["col_gearId"].Visible = false; } }
// Load the data into the DbContext and bind it to the appropriate dataSources for use in the datagridview and comboboxes. private void LoadData() { using (EFTDBEntities context = new EFTDBEntities()) { // Query the appropriate tables and store the results in the DBContext. context.Users.Load(); // Bind the data to the DataSource. userBindingSource.DataSource = context.Users.Local.ToBindingList(); } }
// Load the data into the DbContext and bind it to the appropriate dataSources for use in the datagridview and comboboxes. private void LoadData() { using (EFTDBEntities context = new EFTDBEntities()) { // Query the appropriate tables and store the results in the DBContext. context.Gears.Load(); context.Weapons.Load(); context.Loadouts.Load(); // Bind the data to the DataSource. loadoutBindingSource.DataSource = context.Loadouts.Where(l => l.UserID == currentUser.UserID).ToList(); gearBindingSource.DataSource = context.Gears.Local.ToBindingList(); weaponBindingSource.DataSource = context.Weapons.Local.ToBindingList(); // Create new datasources for each loadoout slot, use lambda expressions to query the database and save the rows that match. BindingSource backpacks = new BindingSource(); backpacks.DataSource = context.Gears.Where(b => b.Slot == "Backpack").ToList(); // Attach the datasource to the correct combobox. The combobox DisplayMember is set to display the Item.Name. cbox_backpack.DataSource = backpacks; BindingSource bodyArmour = new BindingSource(); bodyArmour.DataSource = context.Gears.Where(ba => ba.Slot == "Body armour").ToList(); cbox_bodyArmour.DataSource = bodyArmour; BindingSource earpiece = new BindingSource(); earpiece.DataSource = context.Gears.Where(e => e.Slot == "Earpiece").ToList(); cbox_earpiece.DataSource = earpiece; BindingSource chestRig = new BindingSource(); chestRig.DataSource = context.Gears.Where(cr => cr.Slot == "Chest rig").ToList(); cbox_rig.DataSource = chestRig; BindingSource headwear = new BindingSource(); headwear.DataSource = context.Gears.Where(h => h.Slot == "Headwear").ToList(); cbox_headwear.DataSource = headwear; BindingSource primaryWeapon = new BindingSource(); primaryWeapon.DataSource = context.Weapons.Where(pw => pw.Slot == "Primary").ToList(); cbox_primaryWeapon.DataSource = primaryWeapon; BindingSource secondaryWeapon = new BindingSource(); secondaryWeapon.DataSource = context.Weapons.Where(sw => sw.Slot == "Primary").ToList(); cbox_secondaryWeapon.DataSource = secondaryWeapon; BindingSource holsterWeapon = new BindingSource(); holsterWeapon.DataSource = context.Weapons.Where(hw => hw.Slot == "Secondary").ToList(); cbox_holsterWeapon.DataSource = holsterWeapon; dgv_loadouts.Columns["col_loadoutID"].Visible = false; } }
// Load the data into the DbContext and bind it to the appropriate dataSources for use in the datagridview and comboboxes. private void LoadData() { using (EFTDBEntities context = new EFTDBEntities()) { // Query the appropriate tables and store the results in the DBContext. context.Ammunitions.Load(); context.Calibers.Load(); // Bind the data to the DataSource. ammunitionBindingSource.DataSource = context.Ammunitions.Local.ToBindingList(); caliberBindingSource.DataSource = context.Calibers.Local.ToBindingList(); // Manually hide columns (for some reason just setting the column.visible to false in the designer doesn't work). dgv_ammunition.Columns["col_ammunitionID"].Visible = false; dgv_ammunition.Columns["col_caliberId"].Visible = false; } }
// Perform some validation, check the database for a user with the same username. // If validation succeeds add the new user to the database using an INSERT query from Entity Framework. private void btn_register_Click(object sender, EventArgs e) { using (EFTDBEntities context = new EFTDBEntities()) { if (txt_username.Text == "Username" || txt_username.Text == "" || txt_password.Text == "Password" || txt_password.Text == "Password" || txt_passwordRepeat.Text == "Password Repeat" || txt_passwordRepeat.Text == "" || txt_email.Text == "Email" || txt_email.Text == "") { lbl_errorMessage.Text = "Please make sure all fields are filled."; panel_error.Visible = true; return; } User user = context.Users.Where(u => u.Username == txt_username.Text).FirstOrDefault <User>(); if (user != null) { lbl_errorMessage.Text = "There's already an account with that username. Try logging in."; panel_error.Visible = true; return; } else if (txt_password.Text != txt_passwordRepeat.Text) { lbl_errorMessage.Text = "Passwords do not match."; panel_error.Visible = true; return; } else { User newUser = new User() { Username = txt_username.Text, Password = txt_password.Text, Email = txt_email.Text, Admin = false }; context.Users.Add(newUser); context.SaveChanges(); } } mainMenu.Register(); }
// When a row in the datagridview is clicked, check to make sure it's not the header then store the row's itemID in the currentID variable // Query the database to find the appropriate item and populate the fields with it's data. In the case of comboboxes match the Index with the name. private void dgv_weapons_DoubleClick(object sender, EventArgs e) { btn_save.Enabled = true; if (dgv_users.CurrentRow.Index != -1) { User user = new User(); currentID = Convert.ToInt32(dgv_users.CurrentRow.Cells["col_userID"].Value); Console.WriteLine(currentID.ToString()); using (EFTDBEntities context = new EFTDBEntities()) { user = context.Users.Where(u => u.UserID == currentID).FirstOrDefault(); txt_username.Text = user.Username; txt_email.Text = user.Email; chk_admin.Checked = user.Admin; } btn_delete.Enabled = true; } }
private void btn_save_Click(object sender, EventArgs e) { if (ValidateData()) { using (EFTDBEntities context = new EFTDBEntities()) { Gear gear = new Gear() { GearID = currentID, Name = txt_name.Text, Type = cbox_type.Text, Slot = cbox_slot.Text, Weight = decimal.Parse(txt_weight.Text), Material = txt_material.Text, Class = int.Parse(cbox_class.Text), Durability = int.Parse(txt_durability.Text), Slots = int.Parse(txt_slots.Text), MovementSpeed = int.Parse(txt_movementSpeed.Text), TurnSpeed = int.Parse(txt_turnSpeed.Text), Ergonomics = int.Parse(txt_ergonomics.Text), RicochetChance = float.Parse(txt_ricochetChance.Text), SoundReduction = cbox_soundReduction.Text, BlocksEarpiece = chk_blocksEarpiece.Checked, BlocksEyewear = chk_blocksEyewear.Checked, BlocksFaceCover = chk_blocksFaceCover.Checked, BlocksHeadwear = chk_blocksHeadwear.Checked, BlocksArmour = chk_blocksArmour.Checked }; context.Entry(gear).State = EntityState.Modified; context.SaveChanges(); LoadData(); MessageBox.Show($"Updated {gear.Name} successfully!"); } Clear(); btn_save.Enabled = false; btn_add.Enabled = true; btn_delete.Enabled = false; } }
// Check if the user is sure about deleting, then get the itemID from the currentID and change the entity state to Deleted in the context. // When the changes are saved the database will delete the row using a standard DELETE query. private void btn_delete_Click(object sender, EventArgs e) { if (MessageBox.Show($"Are you sure you want to Delete {txt_username.Text}?", "SQL deletion operation", MessageBoxButtons.YesNo) == DialogResult.Yes) { User user = new User(); using (EFTDBEntities context = new EFTDBEntities()) { user = context.Users.Where(u => u.UserID == currentID).FirstOrDefault(); var entry = context.Entry(user); if (entry.State == EntityState.Detached) { context.Users.Attach(user); } context.Users.Remove(user); context.SaveChanges(); LoadData(); } Clear(); } btn_delete.Enabled = false; btn_save.Enabled = false; }
// If the data is valid, create a new instance of the appropriate entity and input the data. Change the entity state to Modified in the context // When the changes are saved the database will update the row using a standard UPDATE query. private void btn_save_Click(object sender, EventArgs e) { if (ValidateData()) { using (EFTDBEntities context = new EFTDBEntities()) { int caliberID = context.Calibers.Where(c => c.Name == cbox_caliberID.Text).FirstOrDefault().CaliberID; Weapon weapon = new Weapon() { WeaponID = currentID, Name = txt_name.Text, Type = cbox_type.Text, Slot = cbox_slot.Text, Weight = decimal.Parse(txt_weight.Text), VerticalRecoil = int.Parse(txt_verticalRecoil.Text), HorizontalRecoil = int.Parse(txt_horizontalRecoil.Text), Range = int.Parse(txt_range.Text), Ergonomics = int.Parse(txt_ergonomics.Text), FireRate = int.Parse(txt_fireRate.Text), Single = chk_single.Checked, Burst = chk_burst.Checked, Auto = chk_auto.Checked, CaliberID = caliberID }; context.Entry(weapon).State = EntityState.Modified; context.SaveChanges(); LoadData(); MessageBox.Show($"Updated {weapon.Name} successfully!"); } Clear(); btn_save.Enabled = false; btn_add.Enabled = true; btn_delete.Enabled = false; } }