I use the code shown to try to validate my image. Unfortunately, I failed to do that and this is the first time I'm doing a validation for image. Is there anything wrong in my logic? Did I forgot something here? What's the right way in doing these. I hope someone would be able to help me thank you
System.IO.FileNotFoundException: 'Could not find file 'C:\Users\Andrea\source\repos\CapstoneSIMS\CapstoneSIMS\bin\Debug\72EF99A3668CF13820B113EB2E090C37716C9742'.' (I'M getting these error when i tried to insert image)
NOTE: I'm saving the image as image not path and also the datatype is `Varbinary(MAX)` in my database
What I have tried:
<pre lang="c#">
public partial class ADDProduct : MetroForm
{
SIMSProduct _view;
public ADDProduct(SIMSProduct _view)
{
InitializeComponent();
this._view = _view;
}
DataSet ds = new DataSet();
DataTable dt = new DataTable();
byte[] photobyte;
public string CalculateHash(string filename)
{
SHA1CryptoServiceProvider crypt = new SHA1CryptoServiceProvider();
string hash = string.Empty;
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
byte[] checksum = crypt.ComputeHash(fs);
foreach (byte b in checksum)
hash += b.ToString("X2");
}
return (hash);
}
public static bool ImageExists(string filehash)
{
bool result = false;
using (var connection = SQLConnection.GetConnection())
{
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(0) FROM employee_product WHERE ImageHash = @ImageHash", connection))
{
connection.Open();
int imagecount = (int)cmd.ExecuteScalar();
result = imagecount == 0;
connection.Close();
}
}
return (result);
}
private void btn_add_Click(object sender, EventArgs e)
{
_view.ID = txt_id.Text;
filehash = CalculateHash(@"C:\myimagefile.jpg");
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
{
ValidateCode.IsValidCode(txt_code,lbl_code,ds);
string filehash = CalculateHash(pictureBox1.Tag.ToString());
if (lbl_code.Visible == true)
{
CustomNotifcation.Show("CODE ALREADY EXIST", CustomNotifcation.AlertType.error);
lbl_code.Visible = false;
}
else if (ImageExists(pictureBox1.Tag.ToString()))
{
MessageBox.Show("image exists");
return;
}
else
{
using (var select = new SqlCommand("Insert into employee_product (Image, ImageHash, 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("@ImageHash", SqlDbType.VarChar, 50);
select.Parameters["@ImageHash"].Value = filehash;
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)
{
var opnfd = new OpenFileDialog();
opnfd.Filter = "Image Files (*.jpg;*.jpeg;.*.png;)|*.jpg;*.jpeg;.*.png;";
opnfd.Title = "Select Item";
if (opnfd.ShowDialog() == DialogResult.OK)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile(opnfd.FileName);
CalculateHash(opnfd.FileName);
}
}
}