For inserting image in to database we have to convert our image in fileupload in to byte array.
And make sure that our database column will support images. If not change the column type in to image type.
f (fuBrowse.PostedFile.ContentLength > 1) ----->1
{
FileUpload img = (FileUpload)fuBrowse;
Byte[] imgByte = null;
if (fuBrowse.HasFile && fuBrowse.PostedFile != null)
{
HttpPostedFile File = fuBrowse.PostedFile;---->2
imgByte = new Byte[File.ContentLength];--->3
File.InputStream.Read(imgByte, 0, File.ContentLength);
insertCommand.Parameters.AddWithValue("@image", imgByte);
insertCommand.ExecuteNonQuery();
}
}
1.Here we are checking the file upload contain file. fuBrowse is the file upload control.
2. Converting the file in fileupload in to HttpPostedFile
3.And we are converting this HttpPostedFile in to byte array.
4.Passing the byte array to database using commandName.Parameter.addWithValue.