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

  • SEARCH
  • Home
  • 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 9155741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:39:01+00:00 2026-06-17T12:39:01+00:00

I have a Web UI to add / edit / delete subject info and

  • 0

I have a Web UI to add/edit/delete subject info and these data saved in a SQL Server database table named Subject. In that table there are columns such as Id(PK), SubjectCode, SubjectName, Tutor etc…

Id field is the primary key, always unique.

SubjectCode is a varchar field that should be unique (values such as CS1001 etc.)

When insert and update data, I have validated the uniqueness using LINQ (C#) like below.

bool isCodeExist = DbContext.Subject.Any(record => record.SubjectCode == subject.SubjectCode);

if (true == isCodeExist )
{
    // SubjectCode is already exist. Do not proceed.
    return -1;
}
else
{
    // Proceed...
}

This works fine only for inserts.

Sometimes there are errors when update such as update only the tutor name (or other columns) for the subject. Appreciate your help on this.

UPDATE: Complete code section for update operation.

    public int EditSubject(Subject subject)
    {
        using (SMSEntities DbContext= new SMSEntities())
        {
            try
            {                    
                bool isCodeExist = DbContext.Subject.Any(record => record.SubjectCode == subject.SubjectCode);


                if (true == isCodeExist )
                {
                     // SubjectCode is already exist. Do not proceed.
                     return -1;
                }
                else
                {
                    // Create table object 
                    Subject theSubject = new Subject();

                    // Assign values from the domain entity obj from param
                    theSubject.SubjectCode = subject.SubjectCode;
                    theSubject.Tutor = subject.Tutor;       
                    theSubject.LastUpdatedDate = DateTime.Now;
                    // other value assignment also same....

                    // Save subject
                    DbContext.Subjects.Attach(theSubject);
                    DbContext.ObjectStateManager.ChangeObjectState(theSubject, EntityState.Modified);
                    DbContext.SaveChanges();

                    return 0;
                }
            }                
            catch (Exception)
            {
                // Log exception
                throw;
            }
        }
    }
  • 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-17T12:39:02+00:00Added an answer on June 17, 2026 at 12:39 pm

    If it’s only for update,in your predicate you should check for the Id too, something like this:

    bool isCodeExist = DbContext.Subject.Any(record => 
                                             record.SubjectCode == subject.SubjectCode &&
                                             record.Id != subject.Id);
    

    Update:

    But if it’s an UpdateOrInsert :

    public int EditSubject(Subject subject)
    {
        using (SMSEntities DbContext= new SMSEntities())
        {
            try
               {                    
                var theSubject = DbContext.Subject.SingelOrDefault(record => record.SubjectCode == subject.SubjectCode);
    
                if (theSubject != null)
                {
                     // A subject with the same SubjectCode exists.
                     if(existingSubject.Id != Subject.Id)
                     {
                         //It's another subject.
                         return -1;
                     }// else it's the same Subject so we may want to update it.
                }
                else //Subject does not exist.
                {
                    // Create table object 
                    theSubject = new Subject();
                }
                // Assign values from the domain entity obj from param
                theSubject.SubjectCode = subject.SubjectCode;
                theSubject.Tutor = subject.Tutor;       
                theSubject.LastUpdatedDate = DateTime.Now;
                // other value assignment also same....
    
                // Save subject
                DbContext.Subjects.Attach(theSubject);
                DbContext.ObjectStateManager.ChangeObjectState(theSubject, EntityState.Modified);
                DbContext.SaveChanges();
    
                return 0;
            }                
            catch (Exception)
            {
                // Log exception
                throw;
            }
        }
    }
    

    Let me know if it helped.

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

Sidebar

Related Questions

I have a delete button on my web site which I want to add
I have a web service , i add some extra class which have message
I have a UserControl which I have added to my web.config <add tagPrefix=BCF src=~/controls/MyMessageBox.ascx
I have the following code: SPList list = web.Lists[this.ListName]; SPListItem item = list.Items.Add(); now
I have this variable defined in my web.config file : <appSettings> <add key =version
I am using the new System.Web.Optimization and have created a bundle like this: bundles.Add(New
Q1 To configure membership provider, you also have to add section to your web.config
I have a web site which handles files management. User can upload file, add
I have modified my PHP web app to add events to a Google Calendar.
I am creating a web user control for showing Alert on AspxGridView Columns(Delete/Edit) click

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.