i want to upload image and save in database as byte[]
1: working on registration form in which i have other fields also
Code
Name of the Applicant*
Address*
Email*
Photograph*C#
var db = new DatabaseContext();
var applicant = new Applicant();
applicant.Name = txtname.Text;
applicant.Address = txtadress.Text;
applicant.Email = txtemail.Text;
db.Applicant.Add(applicant);
db.SaveChanges();
}Now,how can i implement?

Sachin SinghRank #66 · 26.2K reputationPosted Jan 16, 2023, 8:31 AM EST
first make sure your ProfilePic (image column) property is of byte[] type inside your Applicant Class.
after that do it like this
var db = new DatabaseContext(); var applicant = new Applicant(); applicant.Name = txtname.Text; applicant.Address = txtadress.Text; applicant.Email = txtemail.Text; Stream fs = FileUpload2.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((Int32)fs.Length); applicant.ProfilePic=bytes; db.Applicant.Add(applicant); db.SaveChanges();Vishal JoshiRank #377 · 6.7K reputationPosted Jan 16, 2023, 8:03 AM EST
Hello
You can add below code to upload image to database.
if (FileUpload1.HasFile) { int length = FileUpload1.PostedFile.ContentLength; byte[ ] pic =new byte[length]; FileUpload1.PostedFile.InputStream.Read(pic, 0, length); connection(); //calling connection method to connect database //inserting uploaded image query SqlCommand com = new SqlCommand("insert into ImageTotable " + "(myphoto,name) values (@photo, @photoName)", con); com.Parameters.AddWithValue("@photo", pic); com.Parameters.AddWithValue("@photoName", TextBox1.Text); com.ExecuteNonQuery(); }Thank you.
Pasang TamangRank #449 · 5.6K reputationPosted Jan 16, 2023, 8:42 AM EST
Hi,
Please check below link. This explains how you can upload and save uploaded file into database.
https://www.c-sharpcorner.com/UploadFile/0c1bb2/uploading-images-to-database-using-Asp-Net-C-Sharp/
Regards,
Pasang