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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:04:06+00:00 2026-06-08T07:04:06+00:00

I have a user control with some fields like a textBox called textBoxNombre I

  • 0

I have a user control with some fields like a textBox called textBoxNombre

I have some validation annotations like:

    [Required(ErrorMessage="Debe escribir el Nombre")]
    public String Nombre { get; set; }

This is the simplified code for a button (called buttonAgregar) used to add a new Cliente (Customer):

    private void buttonAgregar_Click(object sender, RoutedEventArgs e)
    {
        var cliente = new Cliente
        {
            Nombre = textBoxNombre.Text,
            Apellido1 = textBoxPrimerApellido.Text,
            Apellido2 = textBoxSegundoApellido.Text,
        };

        db.Clientes.Add(cliente);

        try
        {
            db.SaveChanges();
        }
        catch (System.Data.Entity.Validation.DbEntityValidationException exc)
        {
            String mensaje = "";

            foreach (var validationErrors in exc.EntityValidationErrors)
                foreach (var validationError in validationErrors.ValidationErrors)
                    mensaje += validationError.ErrorMessage + "\n";

            MessageBox.Show(mensaje, "Se han encontrado errores", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }

If I left textBoxNombre empty then the exception is triggered and the message box is shown.
Then I type some text in textBoxNombre but when I click on buttonAgregar again the exception is triggered and the message box is shown again with the same error message.
The buttonAgregar_Click() method don’t get the new value in textBoxNombre.

How can I solve this?

  • 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-08T07:04:08+00:00Added an answer on June 8, 2026 at 7:04 am

    That is because the previously created Cliente instance is still tracked by the context and when you call SaveChanges that entity also get validated. Hence you get the validation exception.

    Detach the entity if you get any validation errors. You may also use new instance of the context each time the button is clicked.

    private void buttonAgregar_Click(object sender, RoutedEventArgs e)
    {
        var cliente = new Cliente
        {
            Nombre = textBoxNombre.Text,
            Apellido1 = textBoxPrimerApellido.Text,
            Apellido2 = textBoxSegundoApellido.Text,
        };
    
        db.Clientes.Add(cliente);
    
        try
        {
            db.SaveChanges();
        }
        catch (System.Data.Entity.Validation.DbEntityValidationException exc)
        {
            String mensaje = "";
    
            foreach (var validationErrors in exc.EntityValidationErrors)
                foreach (var validationError in validationErrors.ValidationErrors)
                    mensaje += validationError.ErrorMessage + "\n";
    
            db.Entry(cliente).State = EntityState.Detached;
    
            MessageBox.Show(mensaje, "Se han encontrado errores", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some control like <ListBox ItemsSource={Binding tests, UpdateSourceTrigger=PropertyChanged} ItemTemplate={StaticResource TestTemplate} /> When user
I would like to have a textbox that lets a user enter some text
I have a user control which uses objects as inner properties (some code is
I have got 5 user controls placed in some Control folder, at run time
I have a custom user control which has some textboxes and some buttons. In
I have created some tables with text fields inside, like in the contacts for
For some user controls, I have this binding: AppLanguage={Binding Path=ApplicationLanguage, Source={x:Static Application.Current}} This works
Hi I have a usercontrol which includes some JavaScript, if I add the control
I am creating an address control which would use some address validation APIs( google/bing
I have a user control where I have two panels, one is on the

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.