Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello Sir/Mam

my project consist of a page on which there are images which are displayed from folders, but i want to change that. I want a code in which i can store a image in sql server 2008 and then retrieve that images and display them.

Help needed urgently.
Updated 10-Jun-12 23:36pm
Nikil0012 11-Jun-12 5:43am    
No u don't get it right, i can't use a button on the page, i have to upload the images which are hardcoded in database. plz help

first u drag file from html controls id as PersonImage put one asp web button in that click event write this-

Int64 intImageSize;
string strImageType;
Stream ImageStream;
//  Gets the Size of the Image
intImageSize = PersonImage.PostedFile.ContentLength;
//  Gets the Image Type
strImageType = PersonImage.PostedFile.ContentType;
//  Reads the Image
ImageStream = PersonImage.PostedFile.InputStream;
byte[,] ImageContent;
int intStatus;
intStatus = ImageStream.Read(ImageContent, 0, intImageSize);
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"));
SqlCommand myCommand = new SqlCommand("sp_person_isp", myConnection);
//  Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter prmPersonImage = new SqlParameter("@PersonImage", SqlDbType.Image);
prmPersonImage.Value = ImageContent;
SqlParameter prmPersonImageType = new SqlParameter("@PersonImageType", SqlDbType.VarChar, 255);
prmPersonImageType.Value = strImageType;
try {
   Response.Write("New person successfully added!");
catch (SqlException SQLexc) {
   Response.Write(("Insert Failed. Error Details are: " + SQLexc.ToString()));
Share this answer
If it is that urgent, I will look it up at Google[^].

This has been done so many times before and there are tons of example on Google. Look at the links below:[^][^]
Share this answer
Very short and concrete to the point answer given in below link:[^]
Share this answer
It's not a brilliant idea to necessarily store images in a DB - they tend to be quite large and can both clog up the DB and cause a lot of bandwidth to be used - particularly if the DB server is not the same computer as your webserver. In that case the image data has to transfer from the DB server to teh webserver before teh web server can serve it to the client. This wastes time, and traffic.
If they are tiny images (avatars maybe, or thumbnails) then it's a good idea, but if they are large images then much, much less so. A better solution is to store them under temporary names, and store the full path to that temporary name in the db.

There is a tip here which may help: A generic Image-From-DB class for ASP.NET[^]
Share this answer
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