Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8182517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:53:08+00:00 2026-06-07T00:53:08+00:00

I need to check for duplicates before saving to the database in the create

  • 0

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; }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:53:09+00:00Added an answer on June 7, 2026 at 12:53 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before inserting data into table i need to check against duplicate records and report
I am having endless troubles with duplicate entries, so I need to check the
I need to check for duplicates. Currently I have items stored in sub folders
I have a database where I need to avoid inserting duplicates. The requirements are:
How can I check for duplicates before inserting into a table when inserting by
Possible Duplicate: Is there a need to do a if(log.isDebugEnabled()) { … } check?
I have an array like the one below, I need to check for duplicates
I need to check for a condition on each page I visit on the
I need to check some settings for all users on Windows clients in the
I need to check if a link (a-tag) is the only content (not just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.