How to insert a photo into a field varbinary(MAX) using c# asp.net
Loading
How to insert a photo into a field varbinary(MAX) using c# asp.net
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Hussain AlShaikhPosted Oct 23, 2023, 3:21 PM
Hi Jose
Thank you for the code, but it didn't work with me, maybe I am using different method.
This is my code below:
private void GetUploadedFile()
{
if (Request.Files.Count > 0 && Request.Files[0].FileName != String.Empty)
{
Request.Files[0].SaveAs(Path.Combine(Server.MapPath(@"../HR/CompanyDocuments"), Request.Files[0].FileName));
string filePath = Request.Files[0].FileName;
byte[] _imageToSave = filePath; (IT SHOWS THE FILE NAME IN THIS VARIABLE BUT IT IS UNDERLINED WITH RED COLOR)
var TheUploadedImage= Request.Files[0].FileName;
// var TheUploadedImage = "data:image/jpg;base64," + Convert.ToBase64String(_imageToSave);
//- Replace nvarcharColumn(MAX) with your column in table.
// Table->nvarcharColunm(MAX) = TheUploadedImage
//- Or display the image as follow
//this.imgPicture.ImageUrl = TheUploadedImage;
string BranchRecID = Session["BranchLogoRecID"].ToString();
string Sql = $"Update BudgetCenter set Scan = @Logo,LogoBr=@LogoBr where recid={BranchRecID}";
string[] paramters = new string[] { "@Logo","@LogoBr" };
string[] vals = new string[] { filePath, TheUploadedImage };
gm.ExecuteSQL(sConnTime, Sql, paramters, vals);
}
}
Jose SaizPosted Oct 22, 2023, 3:39 PM
Have you try the Convert.ToBase64String() instead, saving it to a nVarchar(MAX) column instead of varbinary column
here it the code, assuming you getting the photo/ image from the UPLOAD Field
Happy Coding