Ejemplo n.º 1
0
        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (!IsAnyTextBoxEmpty())
            {
                bool IsConnnected = DatabaseConnection.start();

                bool ISValidStudent = true;

                Student student = new Student();
                try
                {
                    student.FirstName = txtFirstName.Text;
                }
                catch (ArgumentException)
                {
                    ISValidStudent          = false;
                    lblFNameWarning.Visible = true;
                }
                try
                {
                    student.LastName = txtLastName.Text;
                }
                catch (ArgumentException)
                {
                    ISValidStudent          = false;
                    lblLNameWarning.Visible = true;
                }
                try
                {
                    student.RegistrationNo = txtRegNo.Text;
                }
                catch (ArgumentException)
                {
                    ISValidStudent        = false;
                    lblRegWarning.Visible = true;
                }
                try
                {
                    student.Email = txtEmail.Text;
                }
                catch (ArgumentException)
                {
                    ISValidStudent          = false;
                    lblEmailWarning.Visible = true;
                }
                if (ISValidStudent)
                {
                    string StudentGender = "0";
                    if (cmbGender.Text == "Male")
                    {
                        StudentGender = "1";
                    }

                    string day        = cmbDay.Text;
                    string month      = cmbMonth.SelectedIndex.ToString();
                    string year       = cmbYear.Text;
                    string studentDOB = year + " - " + month + " - " + day;

                    bool IsException = false;

                    DatabaseConnection.createStatement("select * from Student where RegistrationNo = '" + txtRegNo.Text + " '");
                    SqlDataReader r = DatabaseConnection.getData();
                    if (r.Read())
                    {
                        IsException = true;
                        MessageBox.Show("This Registration Number already exists");
                    }

                    if (!IsException)
                    {
                        try
                        {
                            DatabaseConnection.createStatement("INSERT INTO Person ( FirstName, LastName, Contact, Email, DateOfBirth, Gender)" +
                                                               " VALUES('" + txtFirstName.Text + "' , '" + txtLastName.Text + "', '" + txtContactNo.Text + "', '" + txtEmail.Text + "', '" + studentDOB + "' ," + StudentGender + "); ");

                            DatabaseConnection.performAction();
                        }
                        catch (SqlException)
                        {
                            IsException           = true;
                            lblDOBwarning.Visible = true;
                        }
                    }
                    if (!IsException)
                    {
                        DatabaseConnection.createStatement("Select @@identity as id from Person");
                        SqlDataReader reader = DatabaseConnection.getData();
                        string        id     = "0";
                        while (reader.Read())
                        {
                            id = (reader["id"].ToString());
                        }

                        DatabaseConnection.createStatement("INSERT INTO Student (Id, RegistrationNo) VALUES (" + id + ", '" + txtRegNo.Text + "') ");
                        DatabaseConnection.performAction();

                        if (!IsException)
                        {
                            MessageBox.Show("Student added");
                            ManageStudent manageStudent = ManageStudent.GetInstance();
                            manageStudent.Show();
                            this.Hide();
                        }
                    }
                }
            }
        }