Click here to Skip to main content
15,909,332 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project I am unable to update an 'image' datatype column named as 'PictureFile'.

The following is my code:
string fileName = ImagePreview.ImageUrl;
int fileLength = PhotoUpload.PostedFile.ContentLength;
int v_mem_id = 158;
byte[] imageBytes = new byte[fileLength];
PhotoUpload.PostedFile.InputStream.Read(imageBytes, 0, fileLength);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tnrestConnectionString"].ConnectionString);
try
{
string updateSql = "UPDATE member " + "SET PictureFile = @a,pictureFile WHERE mem_id= " + v_mem_id + " ";
SqlCommand UpdateCmd = new SqlCommand(updateSql, conn);
UpdateCmd.Parameters.Add("@a", SqlDbType.Image,10, "PictureFile");
UpdateCmd.Parameters["@a"].Value = Convert.ToByte(fileLength).ToString();

conn.Open();
UpdateCmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
}

Can anyone help me out.
Posted

i think u forgot to assign imageBytes = PhotoUpload.FileBytes; ...add following section in code
C#
byte[] imageBytes = new byte[fileLength];
imageBytes = PhotoUpload.FileBytes;
 
Share this answer
 
I think this will be helpful.

Replace all the below code

C#
string fileName = ImagePreview.ImageUrl;
int fileLength = PhotoUpload.PostedFile.ContentLength;
int v_mem_id = 158;
byte[] imageBytes = new byte[fileLength];
PhotoUpload.PostedFile.InputStream.Read(imageBytes, 0, fileLength);


with this code
C#
string fileName = ImagePreview.ImageUrl;
byte[] imageBytes = File.ReadAllBytes(fileName);
UpdateCmd.Parameters.AddWithValue("@a",imageBytes);
 
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