private void Cancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            PersonStudent ps = new PersonStudent();

            ps.ShowDialog();
            this.Close();
        }
Пример #2
0
        // goes to manage students
        private void Mstudents_Click(object sender, EventArgs e)
        {
            PersonStudent st = new PersonStudent();

            st.ShowDialog();
        }
        // when save button clicks
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(conURL);

            // connection opens
            con.Open();



            if (buffer < 0) // for creation of new data
            {
                try
                {
                    // here check whether boxes are empty for not
                    if (String.IsNullOrEmpty(textBoxName.Text) || String.IsNullOrEmpty(textBoxEmail.Text) || String.IsNullOrEmpty(textBoxRegNo.Text))
                    {
                        MessageBox.Show("Fill First Name, Email, Reg No must");
                    }
                    else
                    {
                        // if all validations are true, then insert data
                        if (isalphaTest(textBoxName.Text) && (String.IsNullOrEmpty(textBoxLast.Text) || (!String.IsNullOrEmpty(textBoxLast.Text) && (isalphaTest(textBoxLast.Text)))) && isRegNovalid(textBoxRegNo.Text) && (String.IsNullOrEmpty(textBoxContact.Text) || (!String.IsNullOrEmpty(textBoxContact.Text) && contactNoValid(textBoxContact.Text))))
                        {
                            string val     = "";
                            bool   isCheck = male.Checked;
                            if (isCheck)
                            {
                                val = male.Text;
                            }
                            bool isCheck2 = female.Checked;
                            if (isCheck2)
                            {
                                val = female.Text;
                            }
                            System.Text.RegularExpressions.Regex expr = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
                            if (expr.IsMatch(textBoxEmail.Text))
                            {
                                string cmdText = "INSERT INTO Person (FirstName,LastName, Contact, Email, DateOfBirth, Gender ) VALUES (@FirstName,@LastName, @Contact, @Email, @DateOfBirth, (SELECT Id FROM Lookup WHERE Category = 'Gender' AND Value = @Value))";

                                SqlCommand c = new SqlCommand(cmdText, con);

                                c.Parameters.Add(new SqlParameter("@FirstName", textBoxName.Text));
                                c.Parameters.Add(new SqlParameter("@LastName", textBoxLast.Text));
                                c.Parameters.Add(new SqlParameter("@Contact", textBoxContact.Text));

                                c.Parameters.Add(new SqlParameter("@Email", textBoxEmail.Text));



                                c.Parameters.Add(new SqlParameter("@DateOfBirth", dateTimePicker1.Text));

                                c.Parameters.Add(new SqlParameter("@Value", val));
                                //execute it
                                int result = c.ExecuteNonQuery();
                                if (result < 0)
                                {
                                    MessageBox.Show("Error");
                                }



                                string     cmdText3 = "INSERT INTO Student (Id, RegistrationNo) VALUES((SELECT Id FROM Person WHERE Email = @Email AND Contact = @Contact AND FirstName = @FirstName), @RegistrationNo)";
                                SqlCommand c3       = new SqlCommand(cmdText3, con);
                                c3.Parameters.Add(new SqlParameter("@FirstName", textBoxName.Text));

                                c3.Parameters.Add(new SqlParameter("@Contact", textBoxContact.Text));
                                c3.Parameters.Add(new SqlParameter("@Email", textBoxEmail.Text));
                                c3.Parameters.Add(new SqlParameter("@RegistrationNo", textBoxRegNo.Text));
                                c3.ExecuteNonQuery();

                                // connection closed
                                // show dialog box if added in table of database
                                MessageBox.Show("Successfully Added");
                                con.Close();
                                this.Hide();
                                PersonStudent datap = new PersonStudent();
                                datap.ShowDialog();
                                this.Close(); // close the form
                            }
                            else
                            {
                                throw new ArgumentNullException();
                            }
                        }
                        else
                        {
                            throw new ArgumentNullException();
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Enter Name, Email, RegNo in correct Format!! RegNo pattern be like '2016-CE-72' or 2016-IME-999' Name should be all alphabets, no extra spaces. email should have at least 4 chars before @. contact should have digits");
                }
            }

            else   // for updation of data
            {
                try
                {
                    if (isalphaTest(textBoxName.Text) && (String.IsNullOrEmpty(textBoxLast.Text) || (!String.IsNullOrEmpty(textBoxLast.Text) && (isalphaTest(textBoxLast.Text)))) && isRegNovalid(textBoxRegNo.Text.ToString()) && (String.IsNullOrEmpty(textBoxContact.Text) || (!String.IsNullOrEmpty(textBoxContact.Text) && contactNoValid(textBoxContact.Text))))
                    {
                        string val     = "";
                        bool   isCheck = male.Checked;
                        if (isCheck)
                        {
                            val = male.Text;
                        }
                        bool isCheck2 = female.Checked;
                        if (isCheck2)
                        {
                            val = female.Text;
                        }
                        System.Text.RegularExpressions.Regex expr = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
                        if (expr.IsMatch(textBoxEmail.Text))
                        {
                            string     cmdText2 = "Update Person SET FirstName = @FirstName ,LastName =@LastName , Contact = @Contact, Email = @Email, DateOfBirth = @DateOfBirth, Gender = (SELECT Id FROM Lookup WHERE Category = 'GENDER' AND Value = @Value) WHERE Id = @Id";
                            SqlCommand c2       = new SqlCommand(cmdText2, con);
                            c2.Parameters.Add(new SqlParameter("@Id", buffer));
                            c2.Parameters.Add(new SqlParameter("@FirstName", textBoxName.Text));
                            c2.Parameters.Add(new SqlParameter("@LastName", textBoxLast.Text));
                            c2.Parameters.Add(new SqlParameter("@Contact", textBoxContact.Text));

                            c2.Parameters.Add(new SqlParameter("@Email", textBoxEmail.Text));



                            c2.Parameters.Add(new SqlParameter("@DateOfBirth", dateTimePicker1.Text));

                            c2.Parameters.Add(new SqlParameter("@Value", val));

                            c2.ExecuteNonQuery();


                            string     cmdText3 = "Update Student SET RegistrationNo = @RegistrationNo WHERE Id = @Id";
                            SqlCommand c3       = new SqlCommand(cmdText3, con);
                            c3.Parameters.Add(new SqlParameter("@Id", buffer));
                            c3.Parameters.Add(new SqlParameter("@RegistrationNo", textBoxRegNo.Text));

                            c3.ExecuteNonQuery();



                            MessageBox.Show("Successfully Updated!!");
                            con.Close();
                            this.Hide();
                            PersonStudent datap = new PersonStudent();
                            datap.ShowDialog();
                            this.Close(); // close the form
                        }

                        else
                        {
                            throw new ArgumentNullException();
                        }
                    }
                    else
                    {
                        throw new ArgumentNullException();
                    }
                }

                catch (Exception)
                {
                    MessageBox.Show("Enter Name, Email, RegNo in correct Format!! RegNo pattern be like '2016-CE-72' or 2016-IME-999' Name should be all alphabets, no extra spaces. email should have at least 4 chars before @. contact should have digits");
                }
            }
        }