public ActionResult Import()
 {
     string messageImport = "";
     CSVConvert csv = new CSVConvert();
     try
     {
         HttpPostedFileBase postedFile = Request.Files[0];
         string semester = Request["Semester"];
         if (postedFile != null)
         {
             try
             {
                 string fileExtension = Path.GetExtension(postedFile.FileName);
                 if (fileExtension != ".csv")
                 {
                     messageImport = "Please select the csv file with .csv extension";
                     return Json(new { message = messageImport });
                 }
                 try
                 {
                     using (var sreader = new StreamReader(postedFile.InputStream))
                     {
                         string[] headers = sreader.ReadLine().Split(',');
                         while (!sreader.EndOfStream)
                         {
                             List<string> rows = csv.RegexRow(sreader);
                             //if (!unitOfWork.Classes.CheckExitsClass(getRow.GetClassStudent(rows).Class_ID)
                             //    ||
                             if (unitOfWork.Subject.CheckExitsSubject(getRow.GetSubjectStudent(rows).Subject_ID))
                             {
                                 if (unitOfWork.Students.CheckExitsStudent(getRow.GetStudent(rows, semester).Roll)
                                     && !unitOfWork.Subject.IsExitsSubject(getRow.GetSubjectStudent(rows).Subject_ID))
                                 {
                                     unitOfWork.SubjectStudent.Insert(getRow.GetSubjectStudent(rows));
                                    // unitOfWork.ClassStudent.Insert(getRow.GetClassStudent(rows));
                                 }
                                 else
                                 if (!unitOfWork.Students.CheckExitsStudent(getRow.GetStudent(rows, semester).Roll))
                                 {
                                     unitOfWork.Students.Insert(getRow.GetStudent(rows, semester));
                                     unitOfWork.SubjectStudent.Insert(getRow.GetSubjectStudent(rows));
                                     //unitOfWork.ClassStudent.Insert(getRow.GetClassStudent(rows));
                                 }
                             }
                         }
                     }
                     if (unitOfWork.Save())
                     {
                         messageImport = "Import successfull!";
                     }
                     else
                     {
                         messageImport = "Import Fail!";
                     }
                 }
                 catch (Exception ex)
                 {
                     messageImport = ex.Message;
                 }
                 return Json(new { message = messageImport });
             }
             catch (Exception ex)
             {
                 messageImport = ex.Message;
             }
         }
         else
         {
             messageImport = "Please select the file first to upload.";
         }
     }
     catch
     {
         messageImport = "Please select the file first to upload.";
     }
     return Json(new { message = messageImport });
 }
Exemple #2
0
        public ActionResult Import()
        {
            int        countFail     = 0;
            int        countSuccess  = 0;
            string     messageImport = "";
            CSVConvert csv           = new CSVConvert();

            try
            {
                HttpPostedFileBase postedFile = Request.Files[0];
                string             semester   = Request["Semester"];
                string             campus     = Request["Campus"];
                if (postedFile != null)
                {
                    try
                    {
                        string fileExtension = Path.GetExtension(postedFile.FileName);
                        if (fileExtension.ToLower() != ".csv")
                        {
                            messageImport = "Please select the csv file with .csv extension";
                            return(Json(new { message = messageImport }));
                        }
                        try
                        {
                            using (var sreader = new StreamReader(postedFile.InputStream))
                            {
                                string[] headers = sreader.ReadLine().Split(',');
                                if (headers.Length == 4)
                                {
                                    if (!headers[0].Contains("Roll Number") || !headers[1].Contains("Full Name") || !headers[2].Contains("Email") || !headers[3].Contains("Subject"))
                                    {
                                        messageImport = "Invalid file structure";
                                        return(Json(new { message = messageImport }));
                                    }
                                }
                                else
                                {
                                    messageImport = "The number of columns is invalid";
                                    return(Json(new { message = messageImport }));
                                }
                                int           startRow    = 1;
                                List <string> listAddRoll = new List <string>();
                                while (!sreader.EndOfStream)
                                {
                                    startRow++;
                                    List <string> rows = csv.RegexRow(sreader);
                                    if (!rows[2].Contains("@fpt.edu.vn"))
                                    {
                                        messageImport = "Row " + startRow + ": Email invalid";
                                        return(Json(new { message = messageImport }));
                                    }
                                    if (unitOfWork.Subject.IsExitsSubject(getRow.GetSubjectStudent(rows, semester).Subject_ID))
                                    {
                                        var countAdd = listAddRoll.Where(o => o.Contains(getRow.GetStudent(rows, semester, campus).Roll)).ToList().Count();
                                        if ((unitOfWork.Students.IsExtisStudent(getRow.GetStudent(rows, semester, campus).Roll, semester) &&
                                             !unitOfWork.Subject.IsExitsSubject(getRow.GetSubjectStudent(rows, semester).Subject_ID)) ||
                                            (countAdd > 0))
                                        {
                                            unitOfWork.SubjectStudent.Insert(getRow.GetSubjectStudent(rows, semester));
                                            countSuccess++;
                                        }
                                        else
                                        if (!unitOfWork.Students.IsExtisStudent(getRow.GetStudent(rows, semester, campus).Roll, semester) && countAdd == 0)
                                        {
                                            unitOfWork.Students.Insert(getRow.GetStudent(rows, semester, campus));
                                            unitOfWork.SubjectStudent.Insert(getRow.GetSubjectStudent(rows, semester));
                                            listAddRoll.Add(getRow.GetStudent(rows, semester, campus).Roll);
                                            countSuccess++;
                                        }
                                        else
                                        {
                                            countFail++;
                                        }
                                    }
                                    else
                                    {
                                        messageImport = "Row " + startRow + ": Subject not exist";
                                        return(Json(new { message = messageImport }));
                                    }
                                }
                            }
                            if (unitOfWork.Save())
                            {
                                if (countSuccess != 0)
                                {
                                    messageImport = countFail != 0 ? "Import success " + countSuccess + " students and fail " + countFail + " students!" : "Import success " + countSuccess + " students!";
                                }
                                else
                                {
                                    messageImport = "Import fail " + countFail + " students";
                                }
                            }
                            else
                            {
                                messageImport = "Import Fail!";
                            }
                        }
                        catch (Exception ex)
                        {
                            messageImport = ex.Message;
                        }
                        return(Json(new { message = messageImport }));
                    }
                    catch (Exception ex)
                    {
                        messageImport = ex.Message;
                    }
                }
                else
                {
                    messageImport = "Please select the file first to upload.";
                }
            }
            catch
            {
                messageImport = "Please select the file first to upload.";
            }
            return(Json(new { message = messageImport }));
        }