Here I will explain how insert and retrieve images from database and how to bind images to gridview using asp.net.

Description:

I have worked on one social networking site at that time we save all the images in to directory folder and we save image path into database at that time I got idea to implement concept like inserting images into database and retrieving the images from database and binding images to gridview using asp.net for that we need follow below steps

First Design table like this in your SQL Server database and give name as Image

Column Name

Data Type

Allow Nulls

ImageId

Int(set identity property=true)

No

ImageName

Varchar(50)

Yes

Image

image

Yes

After that Design your aspx page like this

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Inserting images into databse and displaying images with gridview</title>

After Completion of above code we need to add HTTPHandler file to our project to retrieve images from database because we save our images in binary format getting the binary format of data from database it’s easy but displaying is very difficult that’s why we will use HTTPHandler to solve this problem.

Here HTTPHandler is a simple class that allows you to process a request and return a response to the browser. Simply we can say that a Handler is responsible for fulfilling requests from the browser. It can handle only one request at a time, which in turn gives high performance.

Right Click on your project add new HTTPHandler.ashx file and give name as ImageHandler.ashx and write the following code in pagerequest method like this

Here
don’t forgot to set the connection string in web.config file here I am getting
database connection from web.config file for that reason you need to set the
connectionstring in web.config file like this

I am working with Image web gallery so I am trying to use jquery uploadify to upload images and store them in folder and at the same time I need to save them to sql Server database.But I am failing in doing that Can you do me an article regarding that.So that I need to resize the Images while uploading an save them in seperate foldder and the original one's in another folder.And I need to display those Images after loading using list view or gridview as you did.if you give me any ideas that will be very helpful to me.

hii...sir h r u...your site is very nice.....sir i hv used the asp.net3.5..and when we try to add the httphandler file to my project .....then we can no got up this file in ADD new items template ....from where i can add it to my project

hi this is sonu ,i have used this code,codes are running bt images are not comes across the name field,it is unable to show image in data list.except it all things are working properly.i used to generic handler,i am using windows7,visual studio 2010,and .net4.0.whats the problem plz suggest me.sonu new delhi.

Will you please tell me in bigger project which image save method we should use in database using image database or using folder upload classical method. Please describe the difference in performance while comparing both.

i want upload image and save in database then i want to give a link to each image for example :- when i click on img1 it redirect page on its detail page and when i click on img2 it redirect page to its owan page.

I am simanta, i am a web developr.I am stucking with one problem. Please help me sir, I want to dispaly images dynamically in gridview and simultaneously i also want to put some data in 2 textbox. When image change the value inside the textbox should also change. The image should change in a regular time interval.

i am Ajay Yadav. I am trying to upload an image in my website folder and saved its path in my database. I search it on internet but not able to do this. Plz help me. And send me at akylucknow@gmail.com

i wrote the code step by step as you said, but it is showing error "ImageID could not be null"

2) i changed the property value of table as ImageID set to null values true. and i debug it , then it is storing images showing message "saved successfully" but if i check in table , imageID is showing empty. 3) and in gridview control, the image control doesn't showing the stored image, it is showing like breaking image control. why i am getting these errors? give me the solution pls

@kiran,In table design i said we need to set the Identity Property true for ImageID column once we set whenever insert record automatically ImageID column fill with value(like 1,2,3...) otherwise you need to enter values manually. I think you forgot to set the Identity Property for ImageID coulumn and check your images path.

hello sir, ur all articles r very nice n helpfulsir i want to upload multiples images against one UserID in the database and show all images in gridview. i want add button for multiple uploadscan u send me the demo code on my id manindra.tayal@gmai.complz help me

hello suresh,I am sushil. I am able to store the image in database in varbinary format. but the problem is while retrievin it. I am using linq to entity style. so will be there any change in the logic of retrieving. i have tried context.response.binarywrite but its not working

iam chanchal singhmy problem is that all my user not having img in our database only some user have img so that if img is not in our database than only default img is show and img is present then their img show.

hi sir... i am very new to the asp.. presently i am working with the gridview..can we insert data into the gridview with out any ado .net architecture and taking autogenerate colums=false...if yes can you please help me?

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Hi sureshi m a newbie in asp.net..I am a avid user and a great fan of yours,love to read your posts.Please tell me how to give this functionality of inserting comments and displaying them one by one,as is done above..please i have to implement it in my projectRegardsHimanshu

Hii sureshThanks for your reply.I m much elated and thrilled when i saw ur reply for me.ThanksI gone through your post,but i also need to show small profile pictures of the ppl along with their comments???Please do help methanks againregardsHimanshu

Oh suresh,its done.Its 2:30 am morning and i am still Gearing up with your post,trying to add profile of users along with their comments from database,and finally its done.Thanks Suresh for your for your quick reply,which helped me to reach my way.This bespeaks your kindly heart.One more thing i wanna ask you,though i practiced the same in sql server Db,but i am using mysql db in my project,so the same code will work??what is the datatype to be used in mysql for storing images?please always keep in touch.RegardsHimanshu

Why these pics wont display.... i really exhausted.. plz guys somebody help me ... i have followed the steps exactly.. and downloaded these attached file and compared it with mine .. but it still not working ..

the code i working properly but the images don't display .. it shows red "X" instead.

for those who r facing a problem with displaying the image .you just have to add a new webForm and do the retrieve process through it.don't combine both of codes(store image and retrieve image) together in the same form..

copy this code for binding the data in the gridview, and past it in the form load of the new webForm(to retrieve the image) and design a gridview as specified above ... instead of writting the function of binding the data for the gridview in the form of storing the images.

i have used this code,codes are running bt images are not comes across the name field,it is unable to show image in data list.except it all things are working properly.i am using windows7,visual studio 2010,and .net4.0.whats the problem plz suggest me.

@adi...if you are getting cross mark means please check your handler code whether you are getting data from database correctly or not and to display image based on dropdownlist selection means you need to pass image id then get those record details from database and display it on your page.

what is the best way of sending cintrols values from one page to next page and next page to other page by button click and submit it on this page..(as like registration of more than one page )..plz rep it.........

sir, i have an problem when any user form fillup his/her registration purpose, he/she also have to uploaded his/her picture and when he/she logged in the account then he/she can see his/her image as the home section.i want to make profile piccan u tell me sir...............how i will do it

Hi it is very nice article but when run this code take me this is error" Cannot insert the value NULL into column 'ImageId', table 'DressDB .dbo.Image'; column does not allow nulls. INSERT fails.The statement has been terminated."please what i can do thanks alot

I have a similar DB as you have shown except in my assignment I have to use varchar type to store Img path rather then the img it self in the database.

Now using the Grid itself I have to Add the add the img by browsing to the img path and then on "OK" I have to show the img in the grid rather then its path.A hint that I had got from my Faculty is use a File Upload Control.Now I am really saturated trying to figure it out...

you can do 1 thing that somehow try to get the file extension of an uploading file and compare it using if statementif(extnsn=="jpg"){code to be executed}try that .....well i am a beginner but dis could work.....if u find it useful then comment

Hii Suresh Sir..this is jp..can you please tell me how to use the httphandler in our project because when I am retrieving he image from database in the grid view there is no error but it is not showing the image.please reply ASAP....thank you in advance.......

Hellow sir,I implemented ur code but im not getting accurate result. i.e im getting imagename, imageid, but the image is not displaying. In gridview the image is displaying in error image format.Please give me answer what is the reason? and what is the solution. please reply me. thank u somuh.

hi , i m sivi . i having employee login and logout table using oracle. here login and logout time problem. ex 21/01/2013 9:15:10 AM 21/01/2013 6:45:23 PM 21/01/2013 6:46:41 PM. this is prob. i need first entry and last entry then calculate the working hour . ple help me. i m eager to waiting for your replay

HI Suresh. This post was very helpful for me for my project. Now i have a query that u r passing parameter like "cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;" on the page itself but i have created a class and i want to pass the imgbyte parameter to class like "objClass.imgbyte" but it is giving an error. So can u pls help me how to pass parameter of byte[] type to class

I'm really enjoying the theme/design of your site. Do you ever run into any browser compatibility problems? A few of my blog audience have complained about my site not operating correctly in Explorer but looks great in Chrome. Do you have any recommendations to help fix this issue?

Your code is working fine, except image. Instead of grid view I'm using data-list in that i'm getting all data even for image url also like this("ImageHandler.ashx?ImID=5"), data what it is fetching is correct but image is not getting displayed through image handlerfile. This image handler file not get fired only in this datalist. same datahandler I used to display image once i upload a image by that time it is working fine, please guide me how to display image in datalist.

sir ,i have used this code but sir still problem havebeen arise.when we run the webpage it insert the image in database sometimes but it doesn't show imgaes in gridview,no error is occur at the uploading the image

Hello, Can't we put the fileUpload control in the gridView and acces it???? Am not able to bing the image dynamically in gridview by directly putting the fileUpload control in grid View... Sir Please can you help me...

hello sir, there is a problem when retrieve images from database. also you did not explain what namespace should be use. Please tell me the above code and also tell using HttpHandler file does need configure in web.config file. thanks

Hello Sir,I have a problem. I am developing a website using .NET 3.5(lang C#) and WAMP server MySQL as a backend. I want to store and retrieve images from database. I can store the images in database successfully but I am not able to retrieve the images. Plz help me in that sir.

hi suresh sir , here i want to export images in excel format but my images are stored in binary format in database , how can i get it using c# code ............................................................................................................................

ArgumentOutOfRangeExceptionIndex was out of range. Must be non-negative and less than the size of the collection.Parameter name: index every time getting the same error plz help me suresh.why exactely this error is coming?

Instead of loading the DataTable from DB I'm hard coding it and tried to display images which is not working for me. The images are not loading for me. Below is the only code change I've made. Please help me.

Nice blog.. Thanks for providing the information how to insert images into database and how to retrieve and bind images to gridview using asp.net and how save and retrieve images from database using asp.net. Really it is a very useful blog!!

i am using following code to insert image in database........image path is saved in database and also image is saved in picture folder.........but i dont know how to fetch image from database using similar codeif u can please help me

I want to upload a video file using c#.net.Save the uploaded file in SQL database. The uploaded video and its thumbnail shown into grid view. When user click on thumbnail they can play it. Please suggest the better solution.

everything is fine but my sotred images not dispaly in image folder.i hv already use httphandler,ashx file.i hv to follow all ur instruction but all worked successfully but images not display..plz give us positive answer...

sir,I Have two tables and i am displaying the datas using a gridview by using view in sql server i need to implement drop down search thats all are working fine but i need to Change in this by using login form when a user logins his username gets saved automatically by using session but while i am displaying in grid i am getting this username its the id we have given for the corresponding user but related to that id i need to match the correct username thats given in another colomn in login table and needs to display in gridview can u please give me a idea how to implement this. Iam trying from last 2 days not getting please help me....

hello sir, my name is saurabh guptai have a problem can u help me out that problemsir i save the image path in database and image store in a folder but it will not retrieve at the time of page load sir please give me answer thanks sir...

Hi,I was able to load the image on the page using this however the handler.asps/.ashx is running when the current page lifecycle ends (i.e. after the page load completes). In my case I am creating the image control dynamically(there can be multiple image controls) and I need to fetch and bind the images corresponding to each control. Using the above technique only the last image URL gets assigned to the image control, the rest of the images show up blank. Can I get some help on this?

I am working on a windows application that will store customer data along with image,I have done the part of uploading a image. Now I want to write a code that will work as like download link in web applications.How to save uploaded image data back to computer?Here is code i used for upload