-2

Below these code I'm trying to validate if my picturebox image is already exist in database. To be more precise, if user try to insert same image it will validate that "Image Already exist"

Here's the error i got :

(The parameterized query '(@Image varbinary(8000))Select COUNT(*) from employee_product wh' expects the parameter '@Image', which was not supplied.')

What i do wrong here ? or Did i forgot something ? I hope someone would be able to help me. Thank you

public partial class ADDProduct : MetroForm
{
    SIMSProduct _view;
    public ADDProduct(SIMSProduct _view)
    {
        InitializeComponent();
        this._view = _view;                 
    }
    DataTable dt = new DataTable();
    byte[] photobyte;
    string date = DateTime.Now.ToString("MMMM-dd-yyyy");
    public void ValidateImage(byte[] image)
    {
        using (var con = SQLConnection.GetConnection())
        {
                using (var select = new SqlCommand("Select COUNT(*) from employee_product where Image= @Image", con))
                {

                select.Parameters.Add("@Image", SqlDbType.VarBinary).Value = photobyte;
                    using (var sda = new SqlDataAdapter(select))
                    {
                        int count = (int)select.ExecuteScalar();
                        if (count > 0)
                        {
                            lbl_image.Show();
                        }
                    }
                }
        }          
    }
 private void btn_add_Click(object sender, EventArgs e)
    {
        _view.ID = txt_id.Text;
        using (var con = SQLConnection.GetConnection())
        {
            if (string.IsNullOrEmpty(cbox_supplier.Text) || string.IsNullOrEmpty(txt_code.Text) || string.IsNullOrEmpty(txt_item.Text) || string.IsNullOrEmpty(txt_quantity.Text) || string.IsNullOrEmpty(txt_cost.Text) || pictureBox1.Image == null )
            {
                CustomNotifcation.Show("Please input the required fields", CustomNotifcation.AlertType.warning);
            }
            else
            {
                ValidateItem.IsValidItem(txt_code, lbl_code);
                ValidateImage(photobyte);
                if (lbl_code.Visible == true)
                {
                    CustomNotifcation.Show("CODE ALREADY EXIST", CustomNotifcation.AlertType.error);
                    lbl_code.Visible = false;
                }
                else if (lbl_image.Visible == true)
                {
                    CustomNotifcation.Show("IMAGE ALREADY EXIST", CustomNotifcation.AlertType.error);
                    lbl_image.Visible = false;
                }
                else
                {
                    using (var select = new SqlCommand("Insert into employee_product (Image, ID, Supplier, Codeitem, Itemdescription, Date, Quantity, Unitcost) Values (@Image,@ID, @Supplier, @Codeitem, @Itemdescription, @Date, @Quantity, @Unitcost)", con))
                    {
                        var ms = new MemoryStream();
                        pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                        photobyte = ms.GetBuffer();

                        select.Parameters.Add("@Image", SqlDbType.VarBinary).Value = photobyte;
                        select.Parameters.Add("@ID", SqlDbType.VarChar).Value = txt_id.Text;
                        select.Parameters.Add("@Supplier", SqlDbType.VarChar).Value = cbox_supplier.Text;
                        select.Parameters.Add("@Codeitem", SqlDbType.VarChar).Value = txt_code.Text.Trim();
                        select.Parameters.Add("@Itemdescription", SqlDbType.VarChar).Value = txt_item.Text.Trim();
                        select.Parameters.Add("@Date", SqlDbType.VarChar).Value = date;
                        select.Parameters.Add("@Quantity", SqlDbType.Int).Value = txt_quantity.Text.Trim();
                        select.Parameters.Add("@Unitcost", SqlDbType.Int).Value = txt_cost.Text.Trim();
                        select.ExecuteNonQuery();
                        CustomMessage.Show("Message: Item successfully added!", CustomMessage.Messagetype.Success);
                        pictureBox1.Image = null;
                        cbox_supplier.Items.Clear();
                        txt_code.Clear();
                        txt_item.Clear();
                        txt_quantity.Clear();
                        txt_cost.Clear();
                        _view.btn_update.Enabled = false;
                        _view.AddingProduct();
                        this.Close();
                    }
                }                
            }                
        }
    }
    private void pictureBox1_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            ofd.Filter = "Image Files (*.jpg;*.jpeg;.*.png;)|*.jpg;*.jpeg;.*.png;";
            ofd.FilterIndex = 1;
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = Image.FromFile(ofd.FileName);

            }
        }
    }
 }
Hossein
  • 3,083
  • 3
  • 16
  • 33
Anonymous
  • 59
  • 13
  • 2
    This is very bad design about searching binary filed in database just to ensure the binary content is exists on table or not. Store the hash of binary blob (like MD5) when you insert the image in database in separate filed and then when you want to check for existence of image just get MD5 hash of image and query the database for it. – Mojtaba Tajik Sep 16 '18 at 07:03
  • SQL-issues aside: You will find that doing a binary comparison like this will only work if the images are really the same. Re-encoding a jpg, adding a tag etc will make them different. A perfect solution is, however, non-trivial.. – TaW Sep 16 '18 at 07:03
  • @I'm still confused in doing that but thank you – Anonymous Sep 16 '18 at 07:07
  • It is not trivial. Far from being trivial. Even if the content is mostly the same. Comparing encoded binary data makes no sense at all. Just take a look here https://stackoverflow.com/questions/5730631/image-similarity-comparison. Anyway, you could start trying a feature matching approach: www.emgu.com/wiki/index.php/FAST_feature_detector_in_CSharp – ZorgoZ Sep 16 '18 at 07:24
  • We answered this question earlier today, and you got the same answers – TheGeneral Sep 16 '18 at 07:30
  • Possible duplicate of [C# How to check if byte\[\] data is already exist in database](https://stackoverflow.com/questions/52350380/c-sharp-how-to-check-if-byte-data-is-already-exist-in-database) – 41686d6564 stands w. Palestine Sep 16 '18 at 07:53

1 Answers1

1

You can generate some unique values for a file like hash or check-sum and store it along with byte stream in DB, which can be used to check for whether the file exists or not. Normally these mechanisms are not used for this. This only works if file contents are exactly same. Even slightest of variation will be failed to identify the match.

You can find hash like string hash:

using(SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
{
    hash = Convert.ToBase64String(sha1.ComputeHash(byteArray));
}

OR alternatively you can decide to store some alternate information like we normally do. Like file name or user based validations to check whether the file exists.

Hossein
  • 3,083
  • 3
  • 16
  • 33