In this example i am explaining how to Show Display Images In GridView From DataBase In ASP.NET Using C# And VB.NET or showing images stored or saved in SQL Server database in gridview,For this i've already stored images in Database.

For displaying images in gridview we need to create a Handler to read binary data from database.
Right click on solution explorer and Add new item, Pick Generic Handler and name it Handler.ashx.
Write this code in ProcessRequest method

your tutorial is too much helpful...thnks a lot...but just one problem....how to display a particular picture depending upon the id....i have changed the id value as a integer (handler.ashx?id=3)....but the output is 2 same picture with id=3...the no 2 is the rows in my database value ....

Hi AmitI have read your article about displaying image from the database and bind it to the gridview it work fine but i have a problem in my table i have 3 column that display 3 images in the same row when i use the handler.ashx and put all the code in the handler and also in source code behind for my page and i use three images in the templatefield but when i bind the gridview three image is binding the grid but all of them is the same it took always the first column in the table:S plz if you can help me my email is waelwehbe585@hotmail.com waiting ur reply

Thanks a lot for your reply this sample same to work fine but its problem that it goes two time into the database in my case i have to display 3 images per row so its bad for the performance of the application do we have another choice to display the 3 images in one select statement and one handler.ashx many thanks good job waiting ur reply

It's me again and I need another tip about this. Can you suggest me a link on how to properly deploy the project including the settings in web.config, app.config (where to locate it), table adapters etc. Because right now I'm done with everything but the server is throwing errors about my table adapters. I developed the site using only visual web developer 2008 and there's no publish option.

Thanks for the reply. I installed Visual Studio 2008 and build the project I created in visual web developer express 2008. Then I used the Publish utility tool. I transferred some few files to the remote server for testing again, but when I test a page, it is throwing another error about my table adapters, different from the previous error (connection string). I hope you could spare time to look at this:http://www.aseanautobizmag.com/default.aspx

Before anything else, I used objectdatasource to populate data on the tables (used xsd). Do you think I need to create business logic layer for this too? Everything went fine in my development machine but in the deployment (host), its not reading layers (database's tableadapters) and throwing errors when tested. Please let me know if you want to see the whole coding. Thanks again.

Thank you for this code. It has helped a lot. Everything is working for the most part, but I am having two problems. 1)The pictures are not showing on the page, there are red x's in the frame where the pictures are supposed to display. 2)When an image is added to the database it is added twice instead of one time. Please let me know what I can do to fix these errors.

Hi,Thanks for the article, very useful.I have done what you showed here plus made thumbnails instead of normal sized pictures. I need to do a right-click, choose "View Image" and be able to view this image in a new tab. For now, when I do this I have many weird characters. Could you please help me out with sorting this problem. I should probably add some code in the .ashx file but have no idea where to start.Cheers

kartiki said..." Hi,thanks for the code but there is one problem my upload images are not display in the gridview .can u tell me whats the problem.i used ur code same at is it. reply me on my mail id "smilygirl_11@rediffmail.com"

Thanks a lot for this tutorial, it has been very helpful. But I have 1 question: since Imaga data type will be removed in a future version of Microsoft SQL Server, it is nowadays strongly recomended not to use anymore this data type. I tried using your guidelines with Varbinary(max)data type but it doesn't work. Can you do another tutorial with Varbinary(max) data type? Thanks a lot

Hi,Thank you for your endless help.I've tried your codes and they worked well. But I've been trying to do the same with my ASP.NET MVC Application but I couldn't display images in my Application. The .ashx file does not work in my application, would you please instruct me what to do ? Thank youmy email is : foysa1@yahoo.com

Hi Amit, Cel here. Do yo think I can add at least 5 images in the database as long as I declare them all in the handler and code behind? I'm creating another website and the editor wants to have at least 5 images insertions and youtube link section where I can possible use repeater control. Thanks in advance!

Cel here, i downloaded the file for Displaying Images in Multiple Column but its like the code-behind isn't complete., only the 2 handler files. I'm trying to post at least 3 images in a gridview this time. Thanks!

Hi Amit it's me again. I tried the latest downloaded file in the article multiple images and gridview using one Handler but I always get the same image for both column, the 2nd uploaded file takes the column 1 too. I think there's something wrong with my codebehind, as I preview the database, my first uploaded images doesn't attach, only the 2nd one. Maybe I'm missing some command execution. Let me know what you think.

using this code it give me X's instead of pictures in the girdview, I mean it displays red X's(like is unable to open that photo) Any ideas? And also it only runs locally, whenever I try to run it remotely it gives me the error page. Why is this happening? Here is my whole code:<%@ Page language="vb" AutoEventWireup="true" Debug=true %><%@ Import Namespace="System.Drawing" %><%@ Import Namespace="System.IO" %><%@ Import Namespace="System.Drawing.Imaging" %><%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %>

"Specified argument was out of the range of valid values.Parameter name: offset"

Am getting the error like this.... Some pictures are displaying in the Datalist (Instead of GridView am using Datalist).And the browser side..Showing (4 itms remiaing...Downloading picture http://localhost:1213/PL/imgHandler.ashx...

Hello, I am having trouble with the ASP version of the code. Any help is much appreciated.Public Class HandlerImplements IHttpHandler --> GETTING AN ERROR HERE ON the Handler says "class handler must implement read only property is resuable as Boolean forsystem.web.IHTTPhandler..."

Public Sub ProcessRequest(ByVal context As HttpContext)

Dim con As New SqlConnection()con.ConnectionString =ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

I also have a small problem with the code. Everything works fine, I can render the images. But sometimes when i refresh it cant fetch all the images from the database and their image fields are empty. And browser (chrome, firefox) status is "Waiting for localhost". For firefox situation is worse, even if it successfully fetches all images it is still stuck at waiting status.

How can I fix this one? I dont want the browser constantly remain at waiting for connection status when page is visited.

hello sir,This is the problem i have faced with my coding please help me"The parameterized query '(@ID int)Select ImageName,Image from Images where ID =@ID' expects the parameter '@ID', which was not supplied."

Amit, thanks a lot for posting code to help newbies like me - when I try to use your code on vb 2008 express edition some of it wants to update from previous version when this occurs I cant open it am I being extremely stupid? in what /i am doing? only previous experience of vb was using it like a notepad to build a quiz showing 4 possible answers if hte user answered correctly it added 1 to the score and if answered incorectly took a life away so you will see I am real newbie - my email is fizzsdesigns@aol.com

HI amit There is a problem with gridview not displaying images. Not even cross symbols. I had test queried in my database where i could find my images but its not displayed in gridview. One more funny thing is there are no errors displayed neither warnings. Plz help me soon

THANK YOU SO MUCH !! i've been searching for working tutorial for ages ! It works like a charm !I downloaded the version where a parameter is passed to view an image with a specific ID, buti'm facing a problem with this line of code Dim dReader As SqlDataReader =cmd.ExecuteReader()it keeps giving me this error "Failed to convert parameter value from a String to a Int32." what could be the problem and how can i fix it?

THANK YOU SO MUCH !! i've been searching for working tutorial for ages .but whenever i am uploading data ,it is going to upload two times.can u tell me the command to create the table as in your example.