Beispiel #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();
    }
Beispiel #2
0
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string existingStudent = "";
        string failedStudent   = "";
        bool   studentExists   = false;

        try
        {
            List <StudentMajorVO> studentPgmVOList = new List <StudentMajorVO>();
            List <StudentVO>      studentVOList    = new List <StudentVO>();
            List <UserVO>         users            = new List <UserVO>();
            UserDaoImpl           userDaoImpl      = new UserDaoImpl();
            UserVO        userVO                = null;
            IStudentDao   studentDao            = new StudentDaoImpl();
            List <string> existingStudentIDList = studentDao.GetAllStudentIDAsList();
            string        filepath              = Server.MapPath("~/Files/") + Path.GetFileName(fuBulkCourseUpload.PostedFile.FileName);
            if (fuBulkCourseUpload.HasFile)
            {
                fuBulkCourseUpload.SaveAs(filepath);
                string  data = File.ReadAllText(filepath);
                Boolean headerRowHasBeenSkipped = false;
                foreach (string row in data.Split('\n'))
                {
                    if (headerRowHasBeenSkipped)
                    {
                        string programme = "";
                        studentExists = false;
                        if (!string.IsNullOrEmpty(row))
                        {
                            userVO = new UserVO();
                            StudentVO      studentVO      = new StudentVO();
                            StudentMajorVO studentMajorVO = new StudentMajorVO();
                            List <string>  pgmList        = new List <string>();
                            int            i = 0;

                            foreach (string cell in row.Split(','))
                            {
                                if (!studentExists)
                                {
                                    string celltemp = "";
                                    if (cell.Contains("\r"))
                                    {
                                        celltemp = cell.Replace("\r", "");
                                    }
                                    else
                                    {
                                        celltemp = cell;
                                    }
                                    celltemp = celltemp.Trim();
                                    switch (i)
                                    {
                                    case 0:
                                    {
                                        if (existingStudentIDList.Contains(celltemp))
                                        {
                                            existingStudent += celltemp + ",";
                                            studentExists    = true;
                                        }
                                        else
                                        {
                                            studentVO.StudentId      = Convert.ToInt32(celltemp);
                                            studentMajorVO.studentId = Convert.ToInt32(celltemp);;
                                            userVO.StudentID         = Convert.ToInt32(celltemp);
                                        }
                                        break;
                                    }

                                    case 1:
                                    {
                                        studentVO.FirstName = celltemp;
                                        break;
                                    }

                                    case 2:
                                    {
                                        studentVO.LastName = celltemp;
                                        break;
                                    }

                                    case 3:
                                    {
                                        /*  if (celltemp.Contains("&"))
                                         * {
                                         *    foreach (string pgm in celltemp.Split('&'))
                                         *    {
                                         *        pgmList.Add(pgm);
                                         *    }
                                         * }
                                         * else
                                         * {
                                         *    pgmList.Add(celltemp);
                                         * }*/
                                        programme = celltemp;
                                        break;
                                    }

                                    case 4:
                                    {
                                        if (celltemp.Contains("&"))
                                        {
                                            StudentMajorVO sm;
                                            // foreach (string pgm in pgmList)
                                            //{
                                            foreach (string mjr in celltemp.Split('&'))
                                            {
                                                sm             = new StudentMajorVO();
                                                sm.StudentId   = studentMajorVO.StudentId;
                                                sm.ProgrammeID = programme;
                                                sm.MajorID     = mjr.Trim();
                                                studentPgmVOList.Add(sm);
                                            }
                                            // }
                                        }
                                        else
                                        {
                                            StudentMajorVO sm;
                                            // foreach (string pgm in pgmList)
                                            //{
                                            sm             = new StudentMajorVO();
                                            sm.StudentId   = studentMajorVO.StudentId;
                                            sm.ProgrammeID = programme;
                                            sm.MajorID     = celltemp;
                                            studentPgmVOList.Add(sm);
                                            //}
                                        }
                                        break;
                                    }

                                    case 5:
                                    {
                                        if (!celltemp.EndsWith("@ess.ais.ac.nz"))
                                        {
                                            existingStudent += celltemp + ",";
                                            studentExists    = true;
                                        }
                                        else
                                        {
                                            userVO.EmailID = celltemp;
                                        }
                                        break;
                                    }
                                    }
                                }
                                i++;
                            }
                            if (!studentExists)
                            {
                                studentVO.Program = studentPgmVOList;
                                users.Add(userVO);
                                studentVOList.Add(studentVO);
                            }
                        }
                    }
                    headerRowHasBeenSkipped = true;
                }
                if (studentVOList.Count > 0)
                {
                    foreach (StudentVO studentVO in studentVOList)
                    {
                        studentVO.Status          = "Studying";
                        studentVO.CreatedDate     = DateTime.Now;
                        studentVO.LastUpdatedDate = DateTime.Now;
                        string status = (studentBO.AddStudent(studentVO)).ToString();
                        if (status.Contains("fail"))
                        {
                            failedStudent += studentVO.StudentID;
                        }
                    }

                    foreach (StudentMajorVO studentMajorVO in studentPgmVOList)
                    {
                        studentMajorVO.Active = "Yes";
                        string status = (studentMajorBO.AddStudent(studentMajorVO)).ToString();
                        if (status.Contains("fail"))
                        {
                            failedStudent += studentMajorVO.StudentId;
                        }
                    }
                    userDaoImpl.AddStudentUsers(users);

                    string usermessage = " Students upload completed.";
                    if (failedStudent != "")
                    {
                        usermessage += " FailedStudentList " + failedStudent;
                    }
                    if (existingStudent != "")
                    {
                        usermessage += " Skipped existing student. " + existingStudent;
                    }
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('" + usermessage + "');", true);
                }
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "Error Page");
            Response.Write("<script>alert('" + ex.Message + "');</script>");
        }
        finally
        {
            if (DBConnection.conn != null)
            {
                DBConnection.conn.Close();
            }
        }
    }