public ActionResult updateTeacher(edu_course.Models.Teacher tea, int id)
        {
            try
            {
                int           schoolid          = Convert.ToInt32(Session["school"]);
                ClassDBHandle gc                = new ClassDBHandle();
                List <tbl_ClassValidation> list = gc.GetClass(schoolid);
                ViewBag.school = new SelectList(list, "Class_Id", "Name");

                var data = db.Teachers.Where(x => x.Id == id).First();

                if (data != null)
                {
                    data.Name     = tea.Name;
                    data.Contact  = tea.Contact;
                    data.Email    = tea.Email;
                    data.Address  = tea.Address;
                    data.Class_Id = tea.Class_Id;


                    db.Entry(data).State = EntityState.Modified;
                    db.SaveChanges();
                    ViewBag.Message = "Data Successfully Updated";
                    ModelState.Clear();
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = "Not Submitted";
                return(View());
            }
            return(RedirectToAction("viewsteacher", "Home"));
        }
        public ActionResult Profile(edu_course.Models.tbl_TeacherValidation sa)
        {
            try
            {
                edu_course.Models.Teacher teacher = new edu_course.Models.Teacher();
                int userid = Convert.ToInt32((Session["Teacher"]));

                var l = db.loginTables.FirstOrDefault(t => t.UserId == userid);

                if (ModelState.IsValid)
                {
                    if (sa.UserImageFIle != null)
                    {
                        if (l != null)
                        {
                            Session["TeacherName"] = sa.Name;
                            l.UserId          = userid;
                            l.Name            = sa.Name;
                            l.Password        = sa.Password;
                            l.Email           = sa.Email;
                            l.RoleID          = 3;
                            db.Entry(l).State = EntityState.Modified;

                            db.SaveChanges();
                        }
                        string filename  = Path.GetFileNameWithoutExtension(sa.UserImageFIle.FileName);
                        string extension = Path.GetExtension(sa.UserImageFIle.FileName);
                        filename = DateTime.Now.ToString("yymmssff") + extension;



                        teacher.Image    = "~/Content/img/users/" + filename;
                        teacher.Name     = sa.Name;
                        teacher.Email    = sa.Email;
                        teacher.Password = sa.Password;
                        teacher.Id       = userid;



                        if (extension.ToLower() == ".jpg" || extension.ToLower() == ".jpeg" || extension.ToLower() == ".png")
                        {
                            if (sa.UserImageFIle.ContentLength <= 1000000)
                            {
                                db.Entry(teacher).State = EntityState.Modified;



                                string oldImgPath = Request.MapPath(Session["imgPath"].ToString());

                                if (db.SaveChanges() > 0)
                                {
                                    filename = Path.Combine(Server.MapPath("~/Content/img/users/"), filename);
                                    sa.UserImageFIle.SaveAs(filename);
                                    if (System.IO.File.Exists(oldImgPath))
                                    {
                                        System.IO.File.Delete(oldImgPath);
                                    }


                                    ViewBag.Message = "Data Updated";
                                    return(RedirectToAction("Profile"));
                                }
                            }
                            else
                            {
                                ViewBag.msg = "File Size must be Equal or less than 1mb";
                            }
                        }
                        else
                        {
                            ViewBag.msg = "Inavlid File Type";
                        }
                    }

                    //}
                    else
                    {
                        teacher.Image = Session["imgPath"].ToString();
                        if (l != null)
                        {
                            Session["TeacherName"] = sa.Name;
                            l.UserId          = userid;
                            l.Name            = sa.Name;
                            l.Password        = sa.Password;
                            l.Email           = sa.Email;
                            l.RoleID          = 3;
                            db.Entry(l).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        teacher.Name            = sa.Name;
                        teacher.Email           = sa.Email;
                        teacher.Password        = sa.Password;
                        teacher.Id              = userid;
                        db.Entry(teacher).State = EntityState.Modified;

                        if (db.SaveChanges() > 0)
                        {
                            ViewBag.Message = "Data Updated";

                            return(RedirectToAction("Profile"));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = "Not Updated";
                return(View());
            }
            return(View());
        }
        public ActionResult addteacher(tbl_TeacherValidation teacher)
        {
            try
            {
                int           schoolid = Convert.ToInt32(Session["school"]);
                ClassDBHandle gc       = new ClassDBHandle();

                List <tbl_ClassValidation> list = gc.GetClass(schoolid);
                ViewBag.school = new SelectList(list, "Class_Id", "Name");
                teacher.Reg_No = RegNo(teacher);

                edu_course.Models.Teacher a = new edu_course.Models.Teacher();

                var userWithSameEmail = db.Teachers.Where(m => m.Email == teacher.Email).SingleOrDefault(); //checking if the emailid already exits for any user
                if (userWithSameEmail == null)
                {
                    string filename  = Path.GetFileNameWithoutExtension(teacher.UserImageFIle.FileName);
                    string extension = Path.GetExtension(teacher.UserImageFIle.FileName);
                    filename = DateTime.Now.ToString("yymmssff") + extension;

                    a.Image = "~/Content/img/users/" + filename;
                    //image ko folder me save krwanay ke leye
                    filename = Path.Combine(Server.MapPath("~/Content/img/users/"), filename);
                    teacher.UserImageFIle.SaveAs(filename);
                    a.Class_Id  = teacher.Class_Id;
                    a.School_Id = Convert.ToInt32(Session["school"]);
                    a.Name      = teacher.Name;
                    a.Reg_No    = teacher.Reg_No;
                    a.Address   = teacher.Address;
                    a.Email     = teacher.Email;
                    a.Contact   = teacher.Contact;
                    a.Password  = teacher.Password;

                    a.JoiningDate = DateTime.Now;

                    db.Teachers.Add(a);

                    db.SaveChanges();
                }

                else
                {
                    ViewBag.Message = "User with this Email Already Exist";
                    return(View());
                }


                int        teacherlatestid = a.Id;
                loginTable l = new loginTable();
                l.UserId   = teacherlatestid;
                l.Name     = a.Name;
                l.Password = a.Password;
                l.RoleID   = 3;
                l.Email    = a.Email;
                db.loginTables.Add(l);
                db.SaveChanges();
                ModelState.Clear();
                ViewBag.Message = "Data Submitted";
            }
            catch (Exception ex)
            {
                ViewBag.Message = "Not Submitted";
                return(View());
            }

            return(View());
        }