public ActionResult CreateBulkEnrollment(HttpPostedFileBase file)
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.UnitOffering))
            {
                return(RedirectToPermissionDenied());
            }

            //Create data which needs to be outside the try-catch block
            FileCSV      data        = null;
            int          uploadCount = 0;
            int          failCount   = 0;
            Downloadable errorFile   = null;

            // Enter a try-catch block to make sure any exceptions are caught
            try
            {
                // Decode the CSV file
                data = new FileCSV(file);

                // Make sure the headers are correct
                // This will throw an exception if not
                data.ValidateHeaders(new string[]
                {
                    "Unit",           // 0
                    "TeachingPeriod", // 1
                    "Year",           // 2
                    "Student"         // 3
                });

                // Loop through each row of data
                // Generate the list of results
                foreach (string[] row in data.Row)
                {
                    try
                    {
                        //var unit = db.GetUnitForName( row[ 0 ] );
                        //if( unit == null )
                        //   throw new DataException( "Unit doesn't exist. " );

                        //var teachingPeriod = db.GetTeachingPeriodForName( row[ 1 ] );
                        //if( teachingPeriod == null )
                        //   throw new DataException( "TeachingPeriod doesn't exist. " );

                        //var year = db.GetYearForYearValue( Int32.Parse( row[ 2 ] ) );
                        //if( year == null )
                        //   throw new DataException( "Year doesn't exist. " );

                        var unitOffering = db.GetUnitOfferingForDetails(row[0], row[1], Int32.Parse(row[2]));
                        if (unitOffering == null)
                        {
                            throw new DataException("UnitOffering doesn't exist. ");
                        }


                        var student = db.GetUserForUsername(row[3]);
                        if (student == null)
                        {
                            throw new DataException("Convenor doesn't exist. ");
                        }


                        EnrollmentProcessor.InsertEnrollmentModel(unitOffering.UnitOfferingId, student.UserId);
                        data.SetComment(row, "");
                        uploadCount++;
                    }
                    catch (Exception e)
                    {
                        data.SetComment(row, e.Message);
                        failCount++;
                    }
                }

                // Generate and record the error file, if required
                if (failCount > 0)
                {
                    errorFile = Downloadable.CreateCSV(data.GenerateErrorFile( ), "errors.csv");
                }
            }
            catch (Exception e)
            {
                // Record error message for View
                TempData["UploadError"] = e.Message;
            }

            // Record item counts for View
            if (uploadCount > 0)
            {
                TempData["UploadCount"] = uploadCount;
            }
            if (failCount > 0)
            {
                TempData["FailCount"] = failCount;
            }
            Session[FileCSV.SessionLabelUploadErrorLog] = (failCount > 0) ? errorFile : null;

            // All file processing has been completed
            // Go to normal create page

            return(RedirectToAction("Index", "UnitOffering"));
        }
        public ActionResult CreateBulk(HttpPostedFileBase file)
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.User))
            {
                return(RedirectToPermissionDenied());
            }

            // Create data which needs to be outside the try-ctach block
            FileCSV      data        = null;
            int          uploadCount = 0;
            int          failCount   = 0;
            Downloadable errorFile   = null;

            // Enter a try-catch block to make sure any exceptions are caught
            try
            {
                // Decode the CSV file
                data = new FileCSV(file);

                // Make sure the headers are correct
                // This will throw an exception if not
                data.ValidateHeaders(new string[] {
                    "Username",  // 0
                    "FirstName", // 1
                    "LastName",  // 2
                    "Email",     // 3
                    "PhoneNo",   // 4
                    "Password"   // 5
                });

                // Loop through each row of data
                // Generate the list of results
                foreach (string[] row in data.Row)
                {
                    // Generate a password salt
                    // Hash the password
                    string passwordSalt = UserLogin.CreatePasswordSalt();
                    string password     = UserLogin.HashPassword(row[5], passwordSalt);

                    // Create the user within the database
                    try
                    {
                        CreateUser(
                            row[0],                  // Username
                            row[1],                  // FirstName
                            row[2],                  // LastName
                            row[3],                  // Email
                            Convert.ToInt32(row[4]), // PhoneNo
                            password,
                            passwordSalt);
                        data.SetComment(row, "");
                        uploadCount++;
                    }
                    catch (Exception e)
                    {
                        data.SetComment(row, e.Message);
                        failCount++;
                    }
                }

                // Generate and record the error file, if required
                if (failCount > 0)
                {
                    errorFile = Downloadable.CreateCSV(data.GenerateErrorFile(), "errors.csv");
                }
            }
            catch (Exception e)
            {
                // Record error message for View
                TempData["UploadError"] = e.Message;
            }

            // Record item counts for View
            if (uploadCount > 0)
            {
                TempData["UploadCount"] = uploadCount;
            }
            if (failCount > 0)
            {
                TempData["FailCount"] = failCount;
            }
            Session[FileCSV.SessionLabelUploadErrorLog] = (failCount > 0) ? errorFile : null;

            // All file processing has been completed
            // Go to the normal create page
            return(RedirectToAction("Create", "User"));
        }