You're not passing the image bytes to the UPDATE
command, but a string containing the word Logo
.
Also: PLEASE avoid creating SQL commands using string concatenation or String.Format
. Use parameterized queries instead!
Also: Do not use an NVARCHAR
field to store the image bytes (unless you create a BASE64 string from them first), but use a VARBINARY
or IMAGE
column instead.
The problem is in the following line:
str = string.Format("update Register set ... ,Logo='{5}' ...", ..., @"Logo", ...);
As you can see, you're formatting a string, but you don't insert the bytes from the image, but the word "Logo".
Assuming the column Logo
was of type IMAGE
or VARBINARY
, I would write something like this:
byte[] photo_array = null;
if (profPic.Image != null)
{
MemoryStream ms = new MemoryStream();
profPic.Image.Save(ms, ImageFormat.Jpeg);
photo_array = ms.GetBuffer();
}
if (photo_array != null)
{
SqlCommand cmd = new SqlCommand("UPDATE Register SET Logo=@logo", connection);
cmd.Parameters.AddWithValue("@logo", imageBytes);
cmd.ExecuteNonQuery();
}