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 7937303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:29:38+00:00 2026-06-03T22:29:38+00:00

I have a dropdownlistfor with values that is not from my database, and when

  • 0

I have a dropdownlistfor with values that is not from my database, and when a user select values that does not exist in my database, I want to save it to the database(and if it already exist, I just use the existing data).

This work fine with some of my data(see code for where it does not work)

This is my repository:

 public void newPerson(Person addperson)
    {
        db.Person.AddObject(addperson);
        db.SaveChanges(); //This wont work
    }

here is my controller:

[HttpPost]
    public ActionResult Create(CreateNKIphase1ViewModel model)
    {
        if (ModelState.IsValid)
        {
            var goalcard = new GoalCard();

            var companyChecker = "";
            var dbCompanies = createNKIRep.GetCustomerByName();
            var addcustomer = new Customer();
            foreach (var existingcustomer in dbCompanies)
            {
                companyChecker = existingcustomer.CompanyName;
                    if (existingcustomer.CompanyName == model.CompanyName)
                    {
                        var customerId = existingcustomer.Id;
                        var selectedCustomerID = createNKIRep.GetByCustomerID(customerId);
                        goalcard.Customer = selectedCustomerID;
                        break;
                    }
            }
            if (companyChecker != model.CompanyName)
            {
                addcustomer.CompanyName = model.CompanyName;
                createNKIRep.newCustomer(addcustomer); //This works!
                goalcard.Customer = addcustomer;
            }

            if (model.PersonName != null)
            {
                var Personchecker = "";
                var dbPersons = createNKIRep.GetPersonsByName();

                foreach (var existingPerson in dbPersons)
                {
                    Personchecker = existingPerson.Name;
                    if (existingPerson.Name == model.PersonName)
                    {
                        var personId = existingPerson.Id;
                        var selectedPersonID = createNKIRep.GetByPersonID(personId);
                        goalcard.Person = selectedPersonID;
                        break;
                    }
                }
                if (Personchecker != model.PersonName)
                {
                    Person newPerson = new Person();
                    newPerson.Name = model.PersonName;
                    createNKIRep.newPerson(newPerson);//Where repository is called
                    goalcard.Person = newPerson;
                }
            }

But when I try to save a new Person I get the following error message:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Even though Person only have Id and name as attributes in my database.

Is there anything wrong in my code, or is it any setting in my database that has to be changed?

Thanks in advance.

  • 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-03T22:29:39+00:00Added an answer on June 3, 2026 at 10:29 pm

    It’s because I use db.SaveChanges(); multiple times in one post. Reducing numbers of time I use db.SaveChanges helped me get rid of the error.

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

Sidebar

Related Questions

I have a dropdown and when I select an item from it, I want
I have a series of DropDowns that I want the user to add to
Maybe that's not exactly the solution i need, but this is what i want
When I have a DropDownList that relevant to Model of view like this: @Html.DropDownListFor(model
I have a mini form that consists of all select boxes and checkbox lists.
I have a ASP.NET webform that, aside from other controls, has a textbox for
I have a gender select field (--Select--, Male, Female) and I'm populating that in
I have a field in the table that contains this value American|African|Asian. I want
I have multiple dropdown list: @Html.DropDownListFor(x => x.HaveColoSpace.SelectedOptions, new SelectList(Model.HaveColoSpace.Options, Value, Text), new {
If I have a drop down list as follows <div class=editor-label> <%= Html.DropDownListFor(model =>

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.