private void submitButton_Click(object sender, EventArgs e) { using (AutoPartsDbContext db = new AutoPartsDbContext()) { try { db.Parts.Add(new AutoPart { Name = nameTextBox.Text, Description = descriptionTextBox.Text, Price = (decimal)Convert.ToDecimal(priceTextBox.Text) }); db.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
private void submitButton_Click(object sender, EventArgs e) { //Various bits of validation var r = new Regex(@"\(?\d{3}\)?-? *\d{3}-? *-?\d{4}"); if (firstNameTextBox.Text != "" && lastNameTextBox.Text != "" && emailTextBox.Text != "" && phoneNumberTextBox.Text != "" && addressTextBox.Text != "" && r.IsMatch(phoneNumberTextBox.Text)) { if (newUser) { using (AutoPartsDbContext db = new AutoPartsDbContext()) { //if new user, add a new user to the db with info from the forms SubmitUser = new User(); SubmitUser.Address = addressTextBox.Text; SubmitUser.FirstName = firstNameTextBox.Text; SubmitUser.LastName = lastNameTextBox.Text; SubmitUser.PhoneNumber = phoneNumberTextBox.Text; SubmitUser.Email = emailTextBox.Text; db.Users.Add(SubmitUser); string itemsToSubmit = null; //Construct a string containing the itmes from the selected items box for (int i = 0; i < selectedItemsListBox.Items.Count; i++) { itemsToSubmit += selectedItemsListBox.Items[i].ToString() + " "; } int count = 0; string total = null; //Gets the total values of each item based off of the value of the counter next to it while (this.Controls["quantLabel" + count] != null) { if (count > MAX_INDEX_OF_LABELS) { break; } else if (this.Controls["quantLabel" + count].Visible == true) { total += this.Controls["quantLabel" + count].Text + " "; count++; } } //Add order to DB using info gained in previous logic and save changes to DB try { db.Orders.Add(new Order { OrderContents = itemsToSubmit, OrderQuantities = total, OrderType = typeOfOrder, //User = SubmitUser, }); } catch { MessageBox.Show("Please select an item to order"); } db.SaveChanges(); } MessageBox.Show("User created and order submitted!"); } //Same thing, but without creating a new user entry else { string itemsToSubmit = null; for (int i = 0; i < selectedItemsListBox.Items.Count; i++) { itemsToSubmit += selectedItemsListBox.Items[i].ToString() + " "; } int count = 0; string total = null; while (this.Controls["quantLabel" + count] != null) { if (count > MAX_INDEX_OF_LABELS) { break; } else if (this.Controls["quantLabel" + count].Visible == true) { total += this.Controls["quantLabel" + count].Text + " "; count++; } } using (AutoPartsDbContext db = new AutoPartsDbContext()) { db.Orders.Add(new Order { OrderContents = itemsToSubmit, OrderQuantities = total, OrderType = typeOfOrder, //User = SubmitUser, }); db.SaveChanges(); } MessageBox.Show("Order submitted!"); } } else { MessageBox.Show("Please fill out all fields."); } }