private void Customers_Load(object sender, EventArgs e)
        {
            //load the customer list method with the data from mysql
            MySqlConnection connection = new MySqlConnection(connec);

            connection.Open();
            string       query3 = "SELECT * FROM btm495.customers ";
            MySqlCommand comm   = connection.CreateCommand();

            comm.CommandText = query3;
            DataTable        table = new DataTable();
            MySqlDataAdapter adapt = new MySqlDataAdapter(query3, connection);
            int row = 0;

            adapt.Fill(table);
            while (row < table.Rows.Count)

            {
                CustomersDA cust = new CustomersDA();
                cust.Customer_id = Convert.ToInt32(table.Rows[row]["Customer_ID"]);
                cust.Company     = (string)table.Rows[row]["Company"];
                cust.Phone       = Convert.ToInt32(table.Rows[row]["Phone"]);
                customer_lstbx.Items.Add(cust.Show_cust());
                row += 1;
            }
        }
Example #2
0
        //this process will generate an id and create a customer but WILL not check for previously created customer, it might generate duplicates
        private void btnNxtSub_Click(object sender, EventArgs e)
        {
            if (txtFirstName.Text == string.Empty ||
                txtLastName.Text == string.Empty ||
                txtCompany.Text == string.Empty ||
                txtBillAdd.Text == string.Empty ||
                txtBillCity.Text == string.Empty ||
                txtBillZipCode.Text == string.Empty ||
                txtBillCountry.Text == string.Empty ||
                txtShipAdd.Text == string.Empty ||
                txtShipCity.Text == string.Empty ||
                txtShipCountry.Text == string.Empty ||
                txtShipZip.Text == string.Empty

                )
            {
                MessageBox.Show("Please fill out all entry fields");
            }
            else
            {
                //validate and convert data types (integer)
                if (long.TryParse(phone_txtbx.Text, out long phone))
                {
                    phone = long.Parse(phone_txtbx.Text);

                    billcode = txtBillZipCode.Text.ToString();
                    shipcode = txtShipZip.Text.ToString();



                    CustomersDA cust = new CustomersDA();

                    cust.Company           = txtCompany.Text;
                    cust.Last_name         = txtLastName.Text;
                    cust.First_name        = txtFirstName.Text;
                    cust.Email             = mail_txtbx.Text;
                    cust.Person_fo_contact = contact_txtbx.Text;
                    cust.Phone             = Convert.ToInt32(phone);
                    cust.Street_bil        = txtBillAdd.Text;
                    cust.City_bil          = txtBillCity.Text;
                    cust.Postal_code_bil   = billcode;
                    cust.Country_bil       = txtBillCountry.Text;
                    cust.City_ship         = txtShipCity.Text;
                    cust.Street_ship       = txtShipAdd.Text;
                    cust.Country_ship      = txtShipCountry.Text;
                    cust.Postal_code_ship  = shipcode;


                    try
                    {
                        //check for the defined variable for the connec information
                        MySqlConnection connections = new MySqlConnection(connec);
                        connections.Open();


                        string       query1 = "INSERT INTO btm495.customers(Company, Person_of_Contact, First_Name, Last_Name, Email, Phone,  Billing_Address_Street, Billing_Address_City, Billing_Address_Country, Billing_Address_Postal_Code,  Shipping_Address_Street, Shipping_Address_City, Shipping_Address_Country, Shipping_Address_Postal_Code) VALUES( @Company, @Person_of_Contact,@First_Name, @Last_Name, @Email, @Phone, @Billing_Address_Street, @Billing_Address_City, @Billing_Address_Country, @Billing_Address_Postal_Code,  @Shipping_Address_Street, @Shipping_Address_City, @Shipping_Address_Country, @Shipping_Address_Postal_Code)";
                        MySqlCommand comm   = connections.CreateCommand();
                        comm.CommandText = query1;
                        // comm.Parameters.AddWithValue("@Customer_ID", cust.Customer_id);
                        comm.Parameters.AddWithValue("@Company", cust.Company);
                        comm.Parameters.AddWithValue("@Person_of_Contact", cust.Person_fo_contact);
                        comm.Parameters.AddWithValue("@First_Name", cust.First_name);
                        comm.Parameters.AddWithValue("@Last_Name", cust.Last_name);
                        comm.Parameters.AddWithValue("@Email", cust.Email);
                        comm.Parameters.AddWithValue("@Phone", cust.Phone);
                        comm.Parameters.AddWithValue("@Billing_Address_Street", cust.Street_bil);
                        comm.Parameters.AddWithValue("@Billing_Address_City", cust.City_bil);
                        comm.Parameters.AddWithValue("@Billing_Address_Country", cust.Country_bil);
                        comm.Parameters.AddWithValue("@Billing_Address_Postal_Code", cust.Postal_code_bil);
                        comm.Parameters.AddWithValue("@Shipping_Address_Street", cust.Street_ship);
                        comm.Parameters.AddWithValue("@Shipping_Address_City", cust.City_ship);
                        comm.Parameters.AddWithValue("@Shipping_Address_Country", cust.Country_ship);
                        comm.Parameters.AddWithValue("@Shipping_Address_Postal_Code", cust.Postal_code_ship);
                        comm.ExecuteNonQuery();
                        connections.Close();
                    }
                    catch
                    { MessageBox.Show("connection error"); }



                    //select the right aircraft

                    /* Random round = new Random();
                     * int airid = round.Next(1, 100000);
                     */

                    selectedItems = new ListBox.SelectedObjectCollection(aircraft_list);
                    selectedItems = aircraft_list.SelectedItems;
                    int selectedIndex = aircraft_list.SelectedIndex;
                    if (selectedIndex != -1)
                    {
                        for (int i = selectedItems.Count - 1; i >= 0; i--)
                        {
                            subjIDii = selectedItems[i].ToString();
                        }
                        subjIDiii = subjIDii.Split(',');
                        subjID    = Convert.ToInt32(subjIDiii[0]);

                        MySqlConnection connection = new MySqlConnection(connec);
                        connection.Open();
                        string           query3 = "SELECT max(Customer_ID) AS cust FROM btm495.customers";
                        MySqlDataAdapter adapte = new MySqlDataAdapter(query3, connection);
                        adapte.Fill(table2);
                        custid = Convert.ToInt32(table2.Rows[0]["cust"]);
                        connection.Close();


                        //insert aircraft info into the aircraft licenses
                        Aircraft_license_class license = new Aircraft_license_class();
                        //    license.Aircraft_license_ID = airid;
                        license.Aircraft_id = subjID;
                        license.Customer_id = custid;



                        connection.Open();
                        string       query2 = "INSERT INTO btm495.aircraft_license( Customer_ID, Aircraft_ID) VALUES((SELECT Customer_ID FROM btm495.customers WHERE (Customer_ID = '" + license.Customer_id + "' )), (SELECT Aircraft_ID FROM btm495.aircrafts WHERE (Aircraft_ID = '" + license.Aircraft_id + "' )))";
                        MySqlCommand comm   = connection.CreateCommand();
                        comm.CommandText = query2;
                        //   comm.Parameters.AddWithValue("@aircraft_license", license.Aircraft_license_ID);
                        comm.ExecuteNonQuery();
                        connection.Close();

                        MessageBox.Show("customer succesfully created ");
                    }
                }
                else
                {
                    MessageBox.Show("enter postal codes and phone as numbers please");
                }


                // to validate that all textboxes are filled out - alex



                this.Hide();
                Create_Sub cs = new Create_Sub();
                cs.Show();
            }
        }