none
better validation error message RRS feed

  • Question

  • Hi,

    i develop a project on vs2012 EXPRESS. I added entity data model to my project. I have a table (say:Table1) and on it a column Column1(varchar(5)).

    when user input   a string value of which length >5 , after dbContex.savechanges() i can show the error message  to user as below.

    Entities ediDB=new Entities();

    Table1 t=new Table1();

    t.Column1="123456";//wrong value

    ediDB.Add(t);

       try
                {
                    ediDB.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    StringBuilder sb = new StringBuilder();

                    foreach (var failure in ex.EntityValidationErrors)
                    {
                        sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
                        foreach (var error in failure.ValidationErrors)
                        {
                            
                            

                            sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                            sb.AppendLine();
                        }
                    }
                    MessageBox.Show(sb.ToString ());
                }

    but also i want to show the wrong value user entered. so i  need to get the information the column Column1 and its wrong value"123456"

    how can i manage?

    Thank you

    Sunday, February 17, 2013 12:01 AM

Answers

  • ValidationErrors is a collection of DbEntityValidationResult. These have an Entry property you can use to get to the entity, it's current values, and original values.

    • Marked as answer by karynch Monday, February 18, 2013 2:27 PM
    Sunday, February 17, 2013 1:52 AM