Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to prevent the user from double registration,meaning when i'm registering the user,the system has to check if the email or phone number exist in the database. which method will i use to perform the validation? please i used peta poco and not entity framework

What I have tried:

this is the actions in the controller
public ActionResult Create()
        {
            var sexlist = new SelectList(new[] { "Male", "Female" });                        
            ViewBag.SexList = sexlist;
            return View();
        }

        
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Member member)
        {
            AchimotaGCDb repo = new AchimotaGCDb();
            var sexlist = new SelectList(new[] { "Male", "Female" });
            ViewBag.SexList = sexlist;

            repo.Insert(member);
            return RedirectToAction("Index");
        }


this is the class

public partial class Member : AchimotaGCDb.Record<Member>  
    {



		[Column] public int MemberId { get; set; }





		[Column] public string FirstName { get; set; }





		[Column] public string LastName { get; set; }





		[Column] public string Phone { get; set; }





		[Column] public string Sex { get; set; }



        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]           
		[Column] public DateTime? DOB { get; set; }





		[Column] public string ActivationCode { get; set; }



	}
Posted
Updated 19-Jun-17 2:16am

Then try to check if the member already exists or not, something like this would work,
C#
AchimotaGCDb repo = new AchimotaGCDb();
var sexlist = new SelectList(new[] { "Male", "Female" });
ViewBag.SexList = sexlist;
 
if(repo.First(x => x.MemberId == member.MemberId) == null) {
   // No exists
   repo.Insert(member);
} else {
   // Exists
}

There are other functions available as well, such as Contains, IndexOf etc, you can try them out as well, the overall logic remains the same — you search for a member with specific details and then if the user does not exist, you add him, otherwise you return; or show an error.
 
Share this answer
 
thanks for the idea,this is what i did,i use peta poco for database entity

AchimotaGCDb repo = new AchimotaGCDb();
            var sexlist = new SelectList(new[] { "Male", "Female" });
            ViewBag.SexList = sexlist;

            if (repo.SingleOrDefault<Member>("SELECT * FROM Member WHERE Phone=@0",member.Phone) == null)
            {
                // No exists
                repo.Insert(member);
                return RedirectToAction("Index");

            }
            else
            {
                ViewBag.Message = "Error. Phone number already exists";
                return View(member);
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900