private void button1_Click(object sender, EventArgs e) { using (AutoPartsDbContext db = new AutoPartsDbContext()) { //Query db to see if submitted info matches an existing user var query = from users in db.Users where users.PhoneNumber == phoneNumberTextBox.Text && users.FirstName == firstNameTextBox.Text select users; if (query.Count() != 0) { //if user exists, create temporary user object with info pulled from db and pass it into form1 User tempUser = new User(); foreach (var item in query) { tempUser.FirstName = item.FirstName; tempUser.LastName = item.LastName; tempUser.Email = item.Email; tempUser.PhoneNumber = item.PhoneNumber; tempUser.Address = item.Address; } var Form1 = new Form1(false, tempUser); this.Hide(); Form1.Show(); } else { MessageBox.Show("No record found that matches submitted data."); } } }
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."); } }
private void partDropDownBox_SelectedIndexChanged(object sender, EventArgs e) { //Get selected index int index = selectedItemsListBox.FindString(partDropDownBox.SelectedItem.ToString()); //if not an invalid selection display counter next to item, pull item from db, add to selected items and display info if(index == -1) { this.Controls["quantLabel" + labelToShow.ToString()].Visible = true; labelToShow++; using (AutoPartsDbContext db = new AutoPartsDbContext()) { if (partDropDownBox.SelectedItem.ToString() == "MotorOil") { var query = from parts in db.Parts where parts.Name == "MotorOil" select parts; foreach (var item in query) { productDescriptionLabel.Text = item.Description; selectedItemsListBox.Items.Add(item.Name + " $" + item.Price.ToString()); orderTotal += item.Price; orderTotalLabel.Text = "$" + orderTotal.ToString(); } } else if (partDropDownBox.SelectedItem.ToString() == "Coolant") { var query = from parts in db.Parts where parts.Name == "Coolant" select parts; foreach (var item in query) { productDescriptionLabel.Text = item.Description; selectedItemsListBox.Items.Add(item.Name + " $" + item.Price.ToString()); orderTotal += item.Price; orderTotalLabel.Text = "$" + orderTotal.ToString(); } } else if (partDropDownBox.SelectedItem.ToString() == "BrakePads") { var query = from parts in db.Parts where parts.Name == "BrakePads" select parts; foreach (var item in query) { productDescriptionLabel.Text = item.Description; selectedItemsListBox.Items.Add(item.Name + " $" + item.Price.ToString()); orderTotal += item.Price; orderTotalLabel.Text = "$" + orderTotal.ToString(); } } else if (partDropDownBox.SelectedItem.ToString() == "Alternator") { var query = from parts in db.Parts where parts.Name == "Alternator" select parts; foreach (var item in query) { productDescriptionLabel.Text = item.Description; selectedItemsListBox.Items.Add(item.Name + " $" + item.Price.ToString()); orderTotal += item.Price; orderTotalLabel.Text = "$" + orderTotal.ToString(); } } else if (partDropDownBox.SelectedItem.ToString() == "SparkPlug") { var query = from parts in db.Parts where parts.Name == "SparkPlug" select parts; foreach (var item in query) { productDescriptionLabel.Text = item.Description; selectedItemsListBox.Items.Add(item.Name + " $" + item.Price.ToString()); orderTotal += item.Price; orderTotalLabel.Text = "$" + orderTotal.ToString(); } } } } }
/* ** Radio buttons removed, these event handlers are redundant. ** private void newUserRadio_CheckedChanged(object sender, EventArgs e) { emailTextBox.ReadOnly = false; addressTextBox.ReadOnly = false; phoneNumberTextBox.ReadOnly = false; } private void existingUserRadio_CheckedChanged(object sender, EventArgs e) { emailTextBox.ReadOnly = true; addressTextBox.ReadOnly = true; phoneNumberTextBox.ReadOnly = true; } * Used for testing before DB existed * private void populateDropDownBox() { partDropDownBox.Items.Add(motorOil.Name); partDropDownBox.Items.Add(transmissionFluid.Name); partDropDownBox.Items.Add(brakePads.Name); partDropDownBox2.Items.Add(motorOil.Name); partDropDownBox2.Items.Add(transmissionFluid.Name); partDropDownBox2.Items.Add(brakePads.Name); partDropDownBox3.Items.Add(motorOil.Name); partDropDownBox3.Items.Add(transmissionFluid.Name); partDropDownBox3.Items.Add(brakePads.Name); partDropDownBox4.Items.Add(motorOil.Name); partDropDownBox4.Items.Add(transmissionFluid.Name); partDropDownBox4.Items.Add(brakePads.Name); } */ private void Form1_Load(object sender, EventArgs e) { // populateDropDownBox(); //Populates combobox with items pulled from DB using (AutoPartsDbContext db = new AutoPartsDbContext()) { var query = from parts in db.Parts select parts; foreach (var item in query) { partDropDownBox.Items.Add(item.Name); } } }