I need to check for duplicates before saving to the database in the create and edit methods inside my controller. Then I need to display a duplicate error message instead of a generic error message.
My definition of a duplicate is:
userid + Code1ID + Code2ID + Code3ID + Code4ID.
Question:
How to check for a duplicate when the combination of the values above already exists?
Table Name
Character:
CharacterID int
UserID int
Code1ID int
Code2ID int
Code3ID int
Code4ID int
Name Varchar(40)
My primary key is composed of UserID + Code1ID + Code2ID + Code3ID + Code4ID.
This guarantees that no duplicates are entered into the database. But the error message I am returning is a general error message.
I need to check for duplicates first and then return a warning message or error message based on the duplication alone.
Here is the edit methods inside the controller
//
// GET: /Character/Edit/5
public ActionResult Edit(int id)
{
Character character = db.Characters.Find(id);
PopulateUserIDDropDownList(character.UserID);
PopulateCode1IDDropDownList(character.Code1ID);
PopulateCode2IDDropDownList(character.Code2ID);
PopulateCode3IDDropDownList(character.Code3ID);
PopulateCode4IDDropDownList(character.Code4ID);
return View(character);
}
//
// POST: /Character/Edit/5
[HttpPost]
public ActionResult Edit(Character character)
{
try
{
if (ModelState.IsValid)
{
db.Entry(character).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
ModelState.AddModelError("", "Generic Error. Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
PopulateUserIDDropDownList(character.UserID);
PopulateCode1IDDropDownList(character.Code1ID);
PopulateCode2IDDropDownList(character.Code2ID);
PopulateCode3IDDropDownList(character.Code3ID);
PopulateCode4IDDropDownList(character.Code4ID);
return View(character);
}
Here is the create methods inside the controller
//
// GET: /Character/Create
public ActionResult Create()
{
PopulateUserIDDropDownList();
PopulateCode1IDDropDownList();
PopulateCode2IDDropDownList();
PopulateCode3IDDropDownList();
PopulateCode4IDDropDownList();
return View();
}
//
// POST: /Character/Create
[HttpPost]
public ActionResult Create(Character character)
{
try
{
if (ModelState.IsValid)
{
db.Characters.Add(character);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
ModelState.AddModelError("", "Generic Error.Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
PopulateUserIDDropDownList(character.UserID);
PopulateCode1IDDropDownList(character.Code1ID);
PopulateCode2IDDropDownList(character.Code2ID);
PopulateCode3IDDropDownList(character.Code3ID);
PopulateCode4IDDropDownList(character.Code4ID);
return View(character);
}
Model
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace myproject.Models
{
public class Character
{
[Key]
public int CharacterID { get; set; }
[Required(ErrorMessage = "User is required.")]
[Display(Name = "User")]
public Guid UserId { get; set; }
[Required(ErrorMessage = "Code1 is required.")]
[Display(Name = "Code1")]
public int Code1ID { get; set; }
[Required(ErrorMessage = "Code2 is required.")]
[Display(Name = "Code2")]
public int Code2ID { get; set; }
[Required(ErrorMessage = "Code3 is required.")]
[Display(Name = "Code3")]
public int Code3ID { get; set; }
[Required(ErrorMessage = "Code4 is required.")]
[Display(Name = "Code4")]
public int Code4ID { get; set; }
[Required(ErrorMessage = "level is required.")]
[Display(Name = "Level")]
public int LevelID { get; set; }
public virtual aspnet_Users Aspnet_User { get; set; }
public virtual Code1 Code1 { get; set; }
public virtual Code2 Code2 { get; set; }
public virtual Code3 Code3 { get; set; }
public virtual Code4 Code4 { get; set; }
}
}
Before saving to DB… search the DB with those values… if you find something… you have a duplicate and you can display the correct message.
When you are editing, you need to exclude for the query the current object being edited.
You can do a Count() on your repository with those IDs as the WHERE part… if result is 1 or more… you have duplicates.