Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I need to upload the image into database and display the image on <asp:Image> and also on report image using linq, Please help me.
Posted
Comments
Gihan Liyanage 29-Aug-14 0:59am    
There are so many solutions on internet, if you google your problem..

1 solution

Select file to save into the database: 

<asp:fileupload runat="server" id="FileUpload1" xmlns:asp="#unknown" />

<asp:button runat="server" id="btnSave" onclick="SaveToTheDatabase" text="Save to the database" xmlns:asp="#unknown" />

<p><asp:label id="lblMessage" runat="server" enableviewstate="false" xmlns:asp="#unknown" /></p>


http://www.dotnetfunda.com/articles/show/1084/saving-images-into-the-database-in-aspnet-and-displaying-to-the-gridvi[^]

C#
protected void SaveToTheDatabase(object sender, EventArgs e)

{

string fileName = FileUpload1.PostedFile.FileName;

int fileLength = FileUpload1.PostedFile.ContentLength;

 

byte[] imageBytes = new byte[fileLength];

FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, fileLength);

 

string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString();

using (SqlConnection conn = new SqlConnection(connStr))

{

string sql = "INSERT INTO ImageUpload (PictureName, PictureFile) VALUES (@pictureName, @pictureFile)";

SqlParameter[] prms = new SqlParameter[2];

prms[0] = new SqlParameter("@pictureName", SqlDbType.VarChar, 50);

prms[0].Value = fileName;

prms[1] = new SqlParameter("@pictureFile", SqlDbType.Image);

prms[1].Value = imageBytes;

using (SqlCommand cmd = new SqlCommand(sql, conn))

{

cmd.Parameters.AddRange(prms);

conn.Open();

cmd.ExecuteNonQuery();

conn.Close();

}

lblMessage.Text = "Picture uploaded successsfully !";

}

}
 
Share this answer
 
v2
Comments
VICK 29-Aug-14 1:05am    
Nice and Helpful link but the HTML content and other posted above is incomplete. Please correct this. and Format it accordingly.

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