Exemple #1
0
        public void loadCustomers()
        {//loading everything from database for load existing customers
            customerCount = 0;
            dbcon         = new DatabaseConnector();
            dbcon.OpenConnection();
            customerCount = dbcon.GetData("SELECT * FROM customer ORDER BY cid ASC").Rows.Count;
            //customerCount = dbcon.dataSetMain.Tables[0].Rows.Count;

            custObject = new CustomerClass[customerCount]; //so now array size will depend on number of records

            for (int i = 0; i < customerCount; i++)        //same as loadseat method in seatclass
            {
                custObject[i]        = new CustomerClass();
                dbcon.dataRowMain    = dbcon.dataSetMain.Tables[0].Rows[i];
                custObject[i].Cid    = Convert.ToInt32(dbcon.dataRowMain[0]);
                custObject[i].Fname  = Convert.ToString(dbcon.dataRowMain[1]);
                custObject[i].Lname  = Convert.ToString(dbcon.dataRowMain[2]);
                custObject[i].Nic    = Convert.ToString(dbcon.dataRowMain[3]);
                custObject[i].Gender = Convert.ToChar(dbcon.dataRowMain[4].ToString());
                custObject[i].Dob    = Convert.ToDateTime(dbcon.dataRowMain[5].ToString());
                custObject[i].Add1   = Convert.ToString(dbcon.dataRowMain[6]);
                custObject[i].Add2   = Convert.ToString(dbcon.dataRowMain[7]);
                custObject[i].Phone  = Convert.ToString(dbcon.dataRowMain[8]);
                custObject[i].Email  = Convert.ToString(dbcon.dataRowMain[9]);
                dbcon.CloseConnection();
            }
        }
        //edit button
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                var controls = new[] { txtFname.Text, txtLname.Text, txtNic.Text, txtAdd1.Text, txtAdd2.Text, txtEmail.Text, txtPhn.Text };
                if (!controls.All(x => string.IsNullOrEmpty(x))) //if its not null or empty do not enter the details to database
                {                                                //Validation^^^^^^^
                    if (dateTimePicker1.Value < DateTime.Today)  //validation for date must be in past
                    {
                        if ((Regex.IsMatch(txtFname.Text, @"^[a-zA-Z]+$")) || (Regex.IsMatch(txtLname.Text, @"^[a-zA-Z]+$")))
                        {
                            customerObject = new CustomerClass();
                            //customerID = comboBox1.SelectedIndex;
                            string cmbText = comboBox1.Text;
                            //im gonna use regex to extract only number(cid) from cmbtext

                            customerID = Convert.ToInt32(Regex.Match(cmbText, @"\d+").Value);

                            customerObject.Fname = txtFname.Text;

                            customerObject.Lname = txtLname.Text;
                            customerObject.Nic   = txtNic.Text;
                            //gender
                            if (rbMale.Checked)
                            {
                                customerObject.Gender = Convert.ToChar(rbMale.Tag);     //male
                            }
                            else
                            {
                                customerObject.Gender = Convert.ToChar(rbFemale.Tag);     //female
                            }
                            customerObject.Dob   = Convert.ToDateTime(dateTimePicker1.Value.ToString("yyyy-MM-dd"));
                            customerObject.Add1  = txtAdd1.Text;
                            customerObject.Add2  = txtAdd2.Text;
                            customerObject.Phone = txtPhn.Text;
                            customerObject.Email = txtEmail.Text;

                            //MessageBox.Show(updCust.Dob.ToString());
                            customerObject.updateDetails(customerID);
                            MessageBox.Show("Details have been successfully modified", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("date should be in past", "Invalid Date", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Make sure first name and last name contain only letters", "Format Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("Please make sure that all fields are filled", "Empty Fields", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception Error occured " + ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }