Exemplo n.º 1
0
    protected void btnAddStudent_Click(object sender, EventArgs e)
    {
        try
        {
            DBConnection.conn.Open();
            SqlCommand cmd   = new SqlCommand("SELECT COUNT(*) FROM dbo.Student WHERE StudentID =" + Convert.ToInt32(txtStudentID.Text.Trim()), DBConnection.conn);
            Int32      count = (Int32)cmd.ExecuteScalar();
            DBConnection.conn.Close();
            if (count > 0)
            {
                Response.Write("<script>alert('StudentID already exists.');</script>");
            }

            else if (!txtEmail.Text.Trim().EndsWith("@ess.ais.ac.nz"))
            {
                Response.Write("<script>alert('Please enter valid email (e.g. [email protected])');</script>");
            }

            DBConnection.conn.Open();
            SqlCommand cmd1   = new SqlCommand("SELECT COUNT(*) FROM dbo.IlmpUser WHERE EmailId ='" + txtEmail.Text.Trim() + "'", DBConnection.conn);
            Int32      count1 = (Int32)cmd1.ExecuteScalar();
            DBConnection.conn.Close();
            if (count1 > 0)
            {
                Response.Write("<script>alert('Email already exists, please check email Student');</script>");
            }
            else
            {
                bool valid = ValidateControls();
                if (valid)
                {
                    int studentID = 0;
                    try
                    {
                        studentID = Int32.Parse(txtStudentID.Text);
                    }
                    catch (ParseException)
                    {
                        Response.Write("<script>alert('Please enter valid studentid');</script>");
                    }
                    string   firstName  = txtFirstName.Text;
                    string   lastName   = txtLastName.Text;
                    string   status     = "Studying";
                    string   emailID    = txtEmail.Text;
                    DateTime createdDTM = DateTime.Now;
                    DateTime updatedDTM = DateTime.Now;

                    StudentVO studentVO = new StudentVO(studentID, firstName, lastName, status, createdDTM, updatedDTM);

                    UserVO userVO = new UserVO();
                    userVO.StudentID = studentID;
                    userVO.StaffID   = 0;
                    userVO.EmailID   = emailID;
                    userVO.Role      = ApplicationConstants.StudentRole;

                    if (lbMajor.Items.Count == 0)
                    {
                        Response.Write("<script>alert('Please choose major');</script>");
                    }

                    else if (studentBO.AddStudent(studentVO))
                    {
                        for (int i = 0; i < lbMajor.Items.Count; i++)
                        {
                            ListItem       lmajor         = lbMajor.Items[i];
                            StudentMajorVO studentMajorVO = new StudentMajorVO(studentID, ddlProgramme.SelectedItem.Value, lmajor.Value, "Yes");

                            StudentMajorBO studentMajorBO = new StudentMajorBO();
                            if (studentMajorBO.AddStudent(studentMajorVO))
                            {
                                Response.Write("<script>alert('Student added successfully');</script>");
                            }
                            else
                            {
                                Response.Write("<script>alert('Error in adding student major');</script>");
                            }
                        }
                        UserBO userBO = new UserBO();
                        userBO.AddUser(userVO);
                    }
                    else
                    {
                        Response.Write("<script>alert('Error in adding student');</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Please enter all student details');</script>");
                }
            }
        }
        catch (CustomException ex)
        {
            Response.Write("<script>alert('" + ex.Message + "');</script>");
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error in adding student');</script>");
            ExceptionUtility.LogException(ex, "Error Page");
        }
        Clear();
    }