コード例 #1
0
        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.");
                }

            }
        }
コード例 #2
0
        public Form1(bool isNewUser, User userInfo)
        {
            InitializeComponent();
             /*       motorOil = new Part("Motor Oil", "Oil for your engine", 20.00m);
            brakePads = new Part("Brake Pads", "Replacement pads for your brakes", 15.00m);
            transmissionFluid = new Part("Transmission Fluid", "Fluid for your automatic transmission", 20.00m);

            */
            emailOrderRadioButton.Checked = true;

            //If it's a new user, open up the forms for user info
            if (isNewUser)
            {
                emailTextBox.ReadOnly = false;
                addressTextBox.ReadOnly = false;
                phoneNumberTextBox.ReadOnly = false;
                newUser = true;

            }
            //If existing user fill the forms with the user's info and grey them out
            else
            {
                emailTextBox.ReadOnly = true;
                addressTextBox.ReadOnly = true;
                phoneNumberTextBox.ReadOnly = true;
                firstNameTextBox.ReadOnly = true;
                lastNameTextBox.ReadOnly = true;
                phoneNumberTextBox.Text = userInfo.PhoneNumber;
                emailTextBox.Text = userInfo.Email;
                addressTextBox.Text = userInfo.Address;
                firstNameTextBox.Text = userInfo.FirstName;
                lastNameTextBox.Text = userInfo.LastName;
                partDropDownBox.Focus();
                newUser = false;
                SubmitUser = userInfo;

            }
        }
コード例 #3
0
        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.");
            }
        }