Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried to make the application and connect it to the SQL Server. but I have a problem with the following line of code.


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace CBIR
{
    public partial class training : Form
    {
        string fileName;
        public training()
        {
            InitializeComponent();
        }

        private void Pilihgbr_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog() { Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|All Files(*.*)|*.*", ValidateNames = true, Multiselect = false })
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    fileName = dialog.FileName;
                    pictureBox1.Image = Image.FromFile(fileName);
                }
            }
        }

        byte[] ConvertImage (Image img)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return ms.ToArray();
            }
        }

        private async void Mulaitraining_Click(object sender, EventArgs e)
        {
            using (cbirEntities db = new cbirEntities())
            {
                tabel_cbir pic = new tabel_cbir() { Nama_Gambar = fileName, Gambar = ConvertImage (pictureBox1.Image)};
                db.tabel_cbir.Add(pic);
                await db.SaveChangesAsync();
                MessageBox.Show("Tersimpan", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void balik_Click(object sender, EventArgs e)
        {
            menuutama m1 = new menuutama();
            m1.Show();
            this.Close();
        }
    }
}


and an error occurred while running the program on the line.


C#
await db.SaveChangesAsync();


the error message is 'EntityValidationError', How to fix it?

Any help will be appreciated. Thanks.


What I have tried:

I tried using try-catch to fix it, but it's still not work.
Posted
Updated 26-Mar-19 0:55am

try ... catch blocks don;t "fix" problems: they let you handle them properly, or at least gracefully.

What you need to do is look at the data you are passing to your DB, compare that to the actual fields in your tables, and work out what data is the wrong type: a name instead of a number perhaps, or an invalid date. Use the debugger and find out what is being passed then compare against the DB design.

When you have that information, you can look at where that data came from, and add validation to ensure that the "bad data" doesn't get that far - it's probably from user into, and that should always be validated and converted to "native types" as quickly as possible so you don't lose user context, and can give them a timely chance to correct it.
 
Share this answer
 
One or more of the properties in your entity are not valid. put a breakpoint on the line that is failing, run the code, and when the debugger stops, inspect your entity to make sure the properties are valid.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900