public ActionResult Delete(int id) { using (OnlineVotingSystemEntities db = new OnlineVotingSystemEntities()) { var result = db.tblResults.Where(a => a.UserId == id).ToList(); foreach (var item in result) { var VoteObtained = db.tblCandidates.Where(a => a.CandidateId == item.CandidateId).Select(a => a.VoteObtained).FirstOrDefault(); tblCandidate tbc = db.tblCandidates.Where(a => a.CandidateId == item.CandidateId).FirstOrDefault(); tbc.VoteObtained = VoteObtained - 1; db.tblResults.Remove(item); db.SaveChanges(); } var tb = db.tblUserRoles.Where(x => x.UserId == id).FirstOrDefault(); db.tblUserRoles.Remove(tb); db.SaveChanges(); tblUser sm = db.tblUsers.Where(x => x.UserId == id).FirstOrDefault(); db.tblUsers.Remove(sm); db.SaveChanges(); return(Json(new { success = true, message = "Deleted Successfully" }, JsonRequestBehavior.AllowGet)); } }
public ActionResult AddOrEdit(UserViewModel uv) { using (OnlineVotingSystemEntities db = new OnlineVotingSystemEntities()) { tblUser tb = new tblUser(); tb.UserName = uv.UserName; tb.Password = OnlineVotingSystemForCollege.Models.EncryptPassword.textToEncrypt(uv.Password); tb.Fullname = uv.FullName; tb.Email = uv.Email; tb.EmailVerification = true; db.tblUsers.Add(tb); db.SaveChanges(); tblUserRole ud = new tblUserRole(); ud.UserId = tb.UserId; ud.RoleId = 1; db.tblUserRoles.Add(ud); db.SaveChanges(); ViewBag.Message = "User Created Successfully"; return(RedirectToAction("ManageUser")); } }
public ActionResult StartVote() { var voters = db.Voters.Where(x => x.IsSelected == false).ToList(); foreach (var aVoter in voters) { aVoter.IsSelected = true; //db.SaveChanges(); } db.SaveChanges(); return(RedirectToAction("Index", "Voter")); }
public ActionResult VoteCast(List <VoteCastingInformation> result) { Guid urlId = (Guid)TempData["UrlID"]; var voter = db.Voters.Where(u => u.UniqueIdentification == urlId).SingleOrDefault(); if (voter != null) { int voterId = voter.VoterId; foreach (VoteCastingInformation vc in result) { vc.VoterId = voterId; db.VoteCastingInformations.Add(vc); } //db.SaveChanges(); Voter vote = (from v in db.Voters where v.VoterId == voterId select v).SingleOrDefault(); vote.IsCommittedVote = true; db.SaveChanges(); } return(Json(new { success = true }, JsonRequestBehavior.AllowGet)); }
public ActionResult ChangePassword(ChangePasswordViewModel cp) { using (OnlineVotingSystemEntities db = new OnlineVotingSystemEntities()) { string username = Session["UserName"].ToString(); var user = db.tblUsers.Where(a => a.UserName == username).FirstOrDefault(); if (user != null) { var OldPassword = OnlineVotingSystemForCollege.Models.EncryptPassword.textToEncrypt(cp.OldPassword); if (user.Password == OldPassword) { if (cp.NewPassword == cp.ConfirmNew) { user.Password = OnlineVotingSystemForCollege.Models.EncryptPassword.textToEncrypt(cp.NewPassword); db.SaveChanges(); ViewBag.Message = "Password Changed"; } else { ViewBag.Message = "New Password and Confirm New Mismatched"; } } else { ViewBag.Message = "Wrong Password"; } } return(View()); } }
public ActionResult UserVerification(String id) { using (OnlineVotingSystemEntities db = new OnlineVotingSystemEntities()) { bool Status = false; db.Configuration.ValidateOnSaveEnabled = false; var IsVerify = db.tblUsers.Where(a => a.ActivationCode == new Guid(id)).FirstOrDefault(); if (IsVerify != null) { IsVerify.EmailVerification = true; db.SaveChanges(); ViewBag.Message = "Email Verification completed"; Status = true; } else { ViewBag.Message = "Invalid Request...Email not verify"; ViewBag.Status = false; } return(View()); } }
public ActionResult Voted(int id) { var electionid = db.tblCandidates.Where(b => b.CandidateId == id).Select(b => b.ElectionId).FirstOrDefault(); var userid = Convert.ToInt32(Session["UserId"]); var isvoted = db.tblResults.Where(a => a.ElectionId == electionid && a.UserId == userid).FirstOrDefault(); if (isvoted != null) { var CandidateName = isvoted.tblCandidate.CandidateName; ViewBag.Action = "You have already Casted Your Vote To" + CandidateName; return(View()); } tblResult tb = new tblResult(); tb.CandidateId = id; tb.ElectionId = electionid; tb.UserId = userid; tb.UserName = Convert.ToString((Session["UserName"])); var votecounted = db.tblCandidates.Where(a => a.CandidateId == id).FirstOrDefault(); var oldvote = votecounted.VoteObtained; votecounted.VoteObtained = oldvote + 1; var candidatename = votecounted.CandidateName; db.tblResults.Add(tb); db.SaveChanges(); ViewBag.Action = "You Have Sucessfully Casted Your Vote " + candidatename; return(View()); }
public ActionResult Verify(int id) { var user = db.tblUsers.Where(a => a.UserId == id).FirstOrDefault(); if (user != null) { SendEmailToUser(user.Email, user.ActivationCode.ToString()); tblIdRequest tb = db.tblIdRequests.Where(a => a.UserId == id).FirstOrDefault(); db.tblIdRequests.Remove(tb); db.SaveChanges(); } return(View("UserVerified")); }
public ActionResult Registration(RegistrationViewModel uv) { using (OnlineVotingSystemEntities db = new OnlineVotingSystemEntities()) { tblUser tbl = db.tblUsers.Where(u => u.UserName == uv.UserName || u.Email == uv.Email).FirstOrDefault(); if (tbl != null) { return(Json(new { success = false, message = "User Already Register" }, JsonRequestBehavior.AllowGet)); } else { tblUser tb = new tblUser(); tb.UserName = uv.UserName; tb.Password = OnlineVotingSystemForCollege.Models.EncryptPassword.textToEncrypt(uv.Password); tb.Fullname = uv.FullName; tb.Email = uv.Email; tb.EmailVerification = false; tb.ActivationCode = Guid.NewGuid(); db.tblUsers.Add(tb); db.SaveChanges(); tblUserRole ud = new tblUserRole(); ud.UserId = tb.UserId; ud.RoleId = 2; db.tblUserRoles.Add(ud); db.SaveChanges(); tblIdRequest tbid = new tblIdRequest(); tbid.UserEmail = tb.Email; tbid.FullName = tb.Fullname; tbid.UserId = tb.UserId; db.tblIdRequests.Add(tbid); db.SaveChanges(); return(Json(new { success = true, message = "Registration Completed . A Email would be send to your email" + tb.Email + "If You are a Valid User" }, JsonRequestBehavior.AllowGet)); } } }
public JsonResult AddEdit(ElectionViewModel pvm) { if (pvm.ElectionId > 0) { tblElection tb = db.tblElections.Where(x => x.ElectionId == pvm.ElectionId).FirstOrDefault(); tb.ElectionName = pvm.ElectionName; tb.ElectionStartDate = pvm.ElectionStartDate; tb.ElectionEndDate = pvm.ElectionEndDate; db.SaveChanges(); return(Json(new { success = true, message = "Updated Successfully" }, JsonRequestBehavior.AllowGet)); } else { tblElection tb = new tblElection(); tb.ElectionName = pvm.ElectionName; tb.ElectionStartDate = pvm.ElectionStartDate; tb.ElectionEndDate = pvm.ElectionEndDate; tb.NoOfCandidate = 0; db.tblElections.Add(tb); db.SaveChanges(); return(Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet)); } }
public ActionResult Add(CandidateViewModel pvm) { tblCandidate tbl = new tblCandidate(); tbl.CandidateName = pvm.CandidateName; tbl.ElectionId = pvm.ElectionId; tbl.Description = pvm.Description; tbl.VoteObtained = 0; HttpPostedFileBase fup = Request.Files["Photo"]; if (fup != null) { tbl.Photo = fup.FileName; fup.SaveAs(Server.MapPath("~/CandidateImages/" + fup.FileName)); } var candidatenum = db.tblElections.Where(a => a.ElectionId == pvm.ElectionId).Select(a => a.NoOfCandidate).FirstOrDefault(); tblElection tb = db.tblElections.Where(a => a.ElectionId == pvm.ElectionId).FirstOrDefault(); tb.NoOfCandidate = candidatenum + 1; db.tblCandidates.Add(tbl); db.SaveChanges(); return(RedirectToAction("AddCandidate")); }
public ActionResult Index(HttpPostedFileBase postedFile) { if (postedFile != null) { try { string fileExtension = Path.GetExtension(postedFile.FileName); //Validate uploaded file and return error. if (fileExtension != ".xls" && fileExtension != ".xlsx") { ViewBag.Message = "Please select the excel file with .xls or .xlsx extension"; return(View()); } string folderPath = Server.MapPath("~/UploadedFiles/"); //Check directory exists else create one if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } //Save file to folder var filePath = folderPath + Path.GetFileName(postedFile.FileName); postedFile.SaveAs(filePath); //Get file extension string excelConString = ""; //Get connection string using extension switch (fileExtension) { //If uploaded file is Excel 1997-2007. case ".xls": excelConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"; break; //If uploaded file is Excel 2007 and above case ".xlsx": excelConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"; break; } //Read data from first sheet of excel into datatable DataTable dt = new DataTable(); excelConString = string.Format(excelConString, filePath); using (OleDbConnection excelOledbConnection = new OleDbConnection(excelConString)) { using (OleDbCommand excelDbCommand = new OleDbCommand()) { using (OleDbDataAdapter excelDataAdapter = new OleDbDataAdapter()) { excelDbCommand.Connection = excelOledbConnection; excelOledbConnection.Open(); //Get schema from excel sheet DataTable excelSchema = GetSchemaFromExcel(excelOledbConnection); //Get sheet name string sheetName = excelSchema.Rows[0]["TABLE_NAME"].ToString(); excelOledbConnection.Close(); //Read Data from First Sheet. excelOledbConnection.Open(); excelDbCommand.CommandText = "SELECT * From [" + sheetName + "]"; excelDataAdapter.SelectCommand = excelDbCommand; //Fill datatable from adapter excelDataAdapter.Fill(dt); excelOledbConnection.Close(); } } } //Insert records to Employee table. using (var db = new OnlineVotingSystemEntities()) { //Loop through datatable and add employee data to employee table. foreach (DataRow row in dt.Rows) { db.Voters.Add(GetVoterFromExcelRow(row)); } db.SaveChanges(); } ViewBag.Message = "Voter Information Imported Successfully."; } //catch (Exception ex) //{ // ViewBag.Message = ex.Message; //} catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } else { ViewBag.Message = "Please select the file first to upload."; } return(View()); }