Example #1
0
        public ActionResult Create(Student stu)
        {
            bool idstudent = false;

            if (ModelState.IsValid)
            {
                var dao = new StudentDAO();
                idstudent = dao.AddStudent(stu);
            }

            return(Json(idstudent, JsonRequestBehavior.AllowGet));
        }
Example #2
0
 public bool AddStudent(Student s1)
 {
     return(sDao.AddStudent(s1));
 }
Example #3
0
        public ActionResult Upload(HttpPostedFileBase FileUpload)
        {
            StudentDAO stu = new StudentDAO();
            var        x   = (UserLogin)Session[Constants.USER_SESSION];

            if (FileUpload != null)
            {
                // tdata.ExecuteCommand("truncate table OtherCompanyAssets");
                if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {
                    string filename   = FileUpload.FileName;
                    string targetpath = Server.MapPath("~/Doc/");
                    FileUpload.SaveAs(targetpath + filename);
                    string pathToExcelFile  = targetpath + filename;
                    var    connectionString = "";
                    if (filename.EndsWith(".xls"))
                    {
                        connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", pathToExcelFile);
                    }
                    else if (filename.EndsWith(".xlsx"))
                    {
                        connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", pathToExcelFile);
                    }

                    var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
                    var ds      = new DataSet();

                    adapter.Fill(ds, "ExcelTable");

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        try
                        {
                            if (ds.Tables[0].Rows[i][1].ToString() != "" && ds.Tables[0].Rows[i][2].ToString() != "" && ds.Tables[0].Rows[i][7].ToString() != "")
                            {
                                Student TU = new Student();
                                TU.UserName   = ds.Tables[0].Rows[i][1].ToString();
                                TU.Name       = ds.Tables[0].Rows[i][2].ToString();
                                TU.Gender     = Boolean.Parse(ds.Tables[0].Rows[i][3].ToString());
                                TU.Born       = DateTime.Parse(ds.Tables[0].Rows[i][4].ToString());
                                TU.Address    = ds.Tables[0].Rows[i][5].ToString();
                                TU.Mobile     = ds.Tables[0].Rows[i][6].ToString();
                                TU.Email      = ds.Tables[0].Rows[i][7].ToString();
                                TU.CMND       = ds.Tables[0].Rows[i][8].ToString();
                                TU.Password   = ds.Tables[0].Rows[i][9].ToString();
                                TU.IDClass    = classid;
                                TU.Status     = true;
                                TU.Position   = "Student";
                                TU.Active     = true;
                                TU.IDLecturer = lectid;
                                TU.CreateBy   = x.UserID;
                                TU.CreateDate = DateTime.Now;
                                stu.AddStudent(TU);
                            }
                            else
                            {
                                ModelState.AddModelError("mess", "Xin điền đầy đủ các trường bắt buộc như tên, địa chỉ, năm sinh, mật khẩu, biệt danh");
                                return(RedirectToAction("Upload", "Students"));
                            }
                        }

                        catch (DbEntityValidationException ex)
                        {
                            foreach (var entityValidationErrors in ex.EntityValidationErrors)
                            {
                                foreach (var validationError in entityValidationErrors.ValidationErrors)
                                {
                                    Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                                }
                            }
                        }
                    }
                    //deleting excel file from folder
                    if ((System.IO.File.Exists(pathToExcelFile)))
                    {
                        System.IO.File.Delete(pathToExcelFile);
                    }
                    ModelState.AddModelError("mess", "Import thành công tất cả sinh viên");
                    return(RedirectToAction("Upload", "Students"));
                }
                else
                {
                    ModelState.AddModelError("mess", "Chỉ cho phép import các loại file excel phiên bản từ 2013 -> 2017");
                    return(RedirectToAction("Upload", "Students"));
                }
            }
            else
            {
                if (FileUpload == null)
                {
                    ModelState.AddModelError("mess", "Xin vui lòng chọn file Excel");
                }
                return(RedirectToAction("Upload", "Students"));
            }
        }
 public ActionResult <int> AddStudent([FromBody] Student Student)
 {
     return(Ok(StudentDAO.AddStudent(Student)));
 }