Esempio n. 1
0
        private void button5_Click(object sender, EventArgs e)
        {
            Advisorr advisorfrm = new Advisorr();

            advisorfrm.Show();
        }
        private void button8_Click(object sender, EventArgs e)
        {
            Advisorr AdvisorForm = new Advisorr();

            AdvisorForm.Show();
        }
        private void Edit_Click(object sender, EventArgs e)
        {
            con.Close();
            con.Open();
            string emailPattern = @"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$"; // Email address pattern
            string phonePattern = @"^03[0-9]{9}$";

            string namepat      = @"^[A-Z]{1}[a-zA-Z\s\'-]*$";
            bool   isEmailValid = Regex.IsMatch(email.Text, emailPattern);
            bool   isPhoneValid = Regex.IsMatch(cont.Text, phonePattern);

            bool          isFNameValid  = Regex.IsMatch(fname.Text, namepat);
            bool          isLNameValid  = Regex.IsMatch(lname.Text, namepat);
            String        Salarypat     = @"^[0-9]*$";
            bool          isValidSalary = Regex.IsMatch(Salary.Text, Salarypat);
            SqlCommand    cmd1          = new SqlCommand("SELECT * FROM PERSON WHERE Email = '" + email.Text + "'", con);
            SqlDataReader reader        = cmd1.ExecuteReader();

            cmd1 = new SqlCommand("SELECT * FROM PERSON WHERE Contact = '" + cont.Text + "'", con);
            SqlDataReader reader2 = cmd1.ExecuteReader();

            if (reader.HasRows)
            {
                con.Close();
                MessageBox.Show("Email already exists.");
            }

            else if (reader2.HasRows)
            {
                con.Close();
                MessageBox.Show("Phone number should be unique.");
            }
            else
            {
                if (!isEmailValid)
                {
                    con.Close();
                    MessageBox.Show("Email is bnot valid");
                }
                else if (!isPhoneValid)
                {
                    con.Close();
                    MessageBox.Show("Phone number is not valid.");
                }
                else if (!isFNameValid || !isLNameValid)
                {
                    con.Close();
                    MessageBox.Show("Name is not valid.");
                }

                else if (!isValidSalary)
                {
                    con.Close();
                    MessageBox.Show("Not valid Salary.");
                }
                else
                {
                    int index = addDis.CurrentCell.RowIndex;
                    addDis.Rows[index].Selected = true;
                    string     id    = addDis.SelectedCells[0].Value.ToString();
                    string     desig = desi.Text.ToString();
                    int        g     = Designation_look(desig);
                    SqlCommand cmd   = new SqlCommand(" UPDATE PERSON SET FirstName = @FirstName,LastName = @LastName ,Contact = @Contact,Email = @Email,DateOfBirth = @DateOfBirth,Gender = @Gender WHERE Id ='" + id + "';", con);
                    cmd.Parameters.AddWithValue("@FirstName", fname.Text);
                    cmd.Parameters.AddWithValue("@LastName", lname.Text);
                    cmd.Parameters.AddWithValue("@Contact", cont.Text);
                    cmd.Parameters.AddWithValue("@Email", email.Text);
                    cmd.Parameters.AddWithValue("@DateOfBirth", DateTime.Parse(dobdate.Text));
                    string genn = gen.Text.ToString();
                    int    gin  = Gender_look(genn);
                    cmd.Parameters.AddWithValue("@Gender", gin);
                    cmd.ExecuteNonQuery();
                    cmd = new SqlCommand(" Update Advisor SET Designation = '" + g + "', Salary= '" + Salary.Text + "'  WHERE Id ='" + id + "';", con);
                    cmd.ExecuteNonQuery();
                    addDis.Rows.RemoveAt(index);
                    addDis.DataSource = dt;
                    con.Close();
                    this.Hide();
                    Advisorr adv = new Advisorr();
                    adv.Show();
                    MessageBox.Show("Data updated.");
                }
            }
        }