0

I am working on creating a web application for Hospital Management where admin need to approve the Patient/Doctor registration. I have created registration table and set status for each record to 0 until admin approves it. I want to change the column to 1 when admin approves the registration. I have written the approve view and once admin click it 0 should change to 1. But I am getting the validation error.

Attaching my code

using AgileProject_DMC.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.Mvc;

namespace AgileProject_DMC.Controllers
{
    public class AdminApproveController : Controller
    {
    // GET: AdminApproving
    public ActionResult PatientDetails()
    {
        using(RegistrationEntities6 dbModel =new RegistrationEntities6() )
        {
            return View(dbModel.PatientRegistrationTables.Where(x => x.IsStatus==0).ToList());
        }
        return View();
    }
    public ActionResult PatientApprove(int id)
    {
        RegistrationEntities6 contextObject = new RegistrationEntities6();
        //contextObject.Database.ExecuteSqlCommand("Update PatientRegistrationTable SET IsStatus = 1 Where PatientId={0 }", id);
        var acceptStatus = contextObject.PatientRegistrationTables.Find(id);
        acceptStatus.IsStatus = 1;
        contextObject.SaveChanges();
        return RedirectToAction("PatientDetails");

and Table.cs file is

namespace AgileProject_DMC.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity;

    public partial class PatientRegistrationTable
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int PatientId { get; set; }

        [DisplayName("First Name")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "This field is required")]
        public string FirstName { get; set; }

        [DisplayName("Last Name")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "This field is required")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "This field is required")]
        public Nullable<int> Age { get; set; }

        [Required(ErrorMessage = "This field is required")]
        public string Gender { get; set; }

        [Required(ErrorMessage = "This field is required")]
        [DisplayName("Contact Number")]
        public Nullable<long> ContactNumber { get; set; }

        [DisplayName("User Name")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "This field is required")]
        public string UserName { get; set; }

        [DataType(DataType.Password)]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Password is required")]
        [MinLength(6, ErrorMessage = "Minimum 6 characters required")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [DisplayName("Confirm Password")]
        [Compare("Password")]
        public string ConfirmPassword { get; set; }

        public Nullable<int> IsStatus { get; set; }
        public string LoginErrorMessage { get; set; }
        }
    }

Error is:

enter image description here

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
shashank
  • 400
  • 8
  • 25
  • This is a question about databases and EF (which version?), not web pages and MVC. You won't get answers if you use the wrong tags. Post the full exception text, not screenshots of it. The error says you should check the `EntityValidationErrors` property which is missing from the screenshot – Panagiotis Kanavos Jun 19 '20 at 06:31
  • The error clearly says the object you're trying to save is invalid. What does it contain? Are any of its required properties empty? – Panagiotis Kanavos Jun 19 '20 at 06:32
  • BTW are you storing a *cleartext password*? That's just begging for a breach and lawsuits. ASP.NET (all versions) already has a secure built-in authentication mechanisms using strong cryptographic hashing. There's *NO* excuse for anyone storing passwords insecurely, not after so many data breaches, clear government guidance against it – Panagiotis Kanavos Jun 19 '20 at 06:35
  • Yes, I have added the validations in table .cs. If i remove all those validations like [Required(AllowEmptyStrings = false, ErrorMessage = "This field is required")], i am able to update the status to 1 – shashank Jun 19 '20 at 06:36
  • If you think you need to store the password so you can send it back to the user, you don't. This bad practice was abandoned over 10 years ago, replaced by password reset links. There were *too many breaches* caused by the password going to a hijacked email account. If your customer insists they want password reminders, ensure you get the request *in writing* so you can present it at court after the inevitable security breach – Panagiotis Kanavos Jun 19 '20 at 06:37
  • `Yes, I have added the validations` well, there's your problem. The data doesn't match the constraints. Find the bad data and fix it, or remove the constraints. And stop storing your own password. Hackers scan SO questions to find programmers posting such vulnerabilities so they can attack their companies – Panagiotis Kanavos Jun 19 '20 at 06:38
  • Does this answer your question? [Validation failed for one or more entities. See 'EntityValidationErrors' property for more details](https://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert) – Panagiotis Kanavos Jun 19 '20 at 06:40
  • If i remove the constraints, they are getting removed in the Registration page aswell. – shashank Jun 19 '20 at 06:40
  • So find the bad data and fix it – Panagiotis Kanavos Jun 19 '20 at 06:41
  • Howdo i do that? I am new to .net. Dont mind if my questions are silly – shashank Jun 19 '20 at 06:42

0 Answers0