0

i'm developing an app for a veterinary clinic in winforms. i have this table named "petOwner" which gives a foreign key to the table "Pets". now when im trying to register a pet i get information of both the owner and the pet from the user,enter code here the owner is saved successfully (savechanges()!=0) but when it comes to pet it gives me the entity validation err on the following code where triple starred:

if (gpbx_ownerInfo.Enabled==true)
        {
            objOwner.name = txt_ownerName.Text;
            objOwner.family = txt_ownerFamily.Text;
            objOwner.mobile = txt_ownerMobile.Text;
            objOwner.tel = txt_ownerTel.Text;
            objOwner.address = rtxt_ownerAdrs.Text;
            objOwner.comment = rtxt_ownerCmnt.Text;
            if (txt_ownerName.Text != "" & txt_ownerFamily.Text != "" & txt_ownerMobile.Text != "" & rtxt_ownerAdrs.Text != "")
            {
                if (objDB.Tbl_ownerInfo.Where(x => x.name == txt_ownerName.Text & x.family == txt_ownerFamily.Text & x.mobile == txt_ownerMobile.Text).ToList().Count == 0)
                {
                    objDB.Tbl_ownerInfo.Add(objOwner);
                }
                else
                {
                    MessageBox.Show("This keeper already exist\nTry finding them using the 'Keeper already Registered' link.");
                }

            }
            else
            {
                MessageBox.Show("Fill the starred items please");
                txt_ownerName.BackColor = Color.MistyRose;
                txt_ownerFamily.BackColor = Color.MistyRose;
                txt_ownerMobile.BackColor = Color.MistyRose;
                rtxt_ownerAdrs.BackColor = Color.MistyRose;
            }


            if (objDB.SaveChanges() != 0)
            {
                gpbx_ownerInfo.Enabled = false;
                objPet.ownerID = objDB.Tbl_ownerInfo.Max(s => s.ID);
                objPet.name = txt_petName.Text;
                objPet.species = txt_petSpecies.Text;
                objPet.breed = txt_petBreed.Text;
                objPet.birthDate = dt_petBDate.Value.Date;
                if (cbox_petGender.Text == "Male")
                {
                    objPet.gender = true;
                }
                else if (cbox_petGender.Text == "Female")
                {
                    objPet.gender = false;
                }
                else
                objPet.dominatingClr = txt_petClr.Text;
                objPet.distinguishingMarks = rtxt_petMarks.Text;
                if (txt_petName.Text != "" & txt_petSpecies.Text != "" & cbox_petGender.Text != "" & txt_petClr.Text != "" & cbox_petGender.Items.Contains(cbox_petGender.Text))
                {
                    if (objDB.Tbl_Pets.Where(p => p.name == txt_petName.Text & p.species == txt_petSpecies.Text & p.breed == txt_petBreed.Text & p.dominatingClr == txt_petClr.Text).ToList().Count == 0)
                    {
                        objDB.Tbl_Pets.Add(objPet);
                        ***if (objDB.SaveChanges() != 0)***
                        {
                            gpbx_ownerInfo.Enabled = true;
                            textCleaner();
                            MessageBox.Show("Pet registered successfully");
                        }

                    }
                    else
                    {
                        gpbx_ownerInfo.Enabled = false;
                        MessageBox.Show("This pet already exists");
                    }

now i know a lot of people don't get this in winforms they usually face it in asp.net and i've searched a lot but every body has given answers on asp here is the exception:enter image description here

Reza
  • 1
  • 2
  • Could you try getting the actual error, it might give more of a clue to what's going wrong here? Here's a post that discusses how to get the "real" error: https://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert Also, do you really have gender = True = Male, or gender = False = Female? (long live the patriarchy!!) I think the reason you are struggling to find this error for WinForms is that very few people actually use WinForms anymore ;P – Richard Hansell Apr 27 '20 at 10:27
  • well i really didn't mean it the way you looked at it :)))))) it could be Male=False & Female=True but there are only two ways to do it :)))) you're right but i'm a windows app student so i have to do it. so thanks anyway @RichardHansell – Reza Apr 27 '20 at 11:15
  • Yeah, sorry about that, it just looked a bit weird in the code. So did you get the underlying error(s), because "entity validation exception" isn't a lot to go on? – Richard Hansell Apr 27 '20 at 11:59
  • yes actually i did, found out i had to check one of my attributes as nullable. Thanks for your help @RichardHansell – Reza Apr 27 '20 at 15:46

0 Answers0