0

I want to Display images into repeater control from Database with the help of ASHX file. I am beginner to ASP.net. It would be really helpful if you can provide me any code example.

EDIT, from comments

I am trying to get images from database into repeater control.

This is what I did in aspx.cs file

cnn.Open(); 
SqlDataAdapter da1 = new SqlDataAdapter("select * from carousel", cnn);
DataTable dt1 = new DataTable(); da1.Fill(dt1);
Rp1.DataSource = dt1;
Rp1.DataBind();

this is my Repaeter

<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ID")%>'/> 

But I am Getting src="system.Byte" in results.

I know This is because the value coming from the database is a byte array representing the actual data of the image. So I want to know about ASHX

brazilianldsjaguar
  • 1,409
  • 1
  • 20
  • 45
Moiz
  • 475
  • 6
  • 18
  • Can you explain what you've already tried to do? And additional comments on how it didn't work? – brazilianldsjaguar Jul 07 '14 at 16:41
  • I am trying to get images from database into repeater control This is what I did in aspx.cs file cnn.Open(); SqlDataAdapter da1 = new SqlDataAdapter("select * from carousel", cnn); DataTable dt1 = new DataTable(); da1.Fill(dt1); Rp1.DataSource = dt1; Rp1.DataBind(); this is my Repaeter But I am Getting src="system.Byte" in result. I know This is because the value coming from the database is a byte array representing the actual data of the image. So I want to know abht ASHX – Moiz Jul 07 '14 at 16:49
  • I put your comment as code into your question -- that way we can see it formatted in a useful manner. Thanks for providing this good information! – brazilianldsjaguar Jul 07 '14 at 17:10
  • What flavor of SQL are you using? I'm assuming SQL Server? If so, what edition (2005, 2008, 2012)? And, perhaps more importantnly, what `DATATYPE` is the column that is holding the image? – brazilianldsjaguar Jul 07 '14 at 17:13
  • 1
    Actually, take a look at this similar question here: http://stackoverflow.com/q/7390983/1245766 – brazilianldsjaguar Jul 07 '14 at 17:16
  • Yes I am using SQL Server 2008 – Moiz Jul 07 '14 at 17:20
  • The data type of image column is "Image" – Moiz Jul 07 '14 at 17:20

1 Answers1

1

The question that I mentioned in the comments has some good examples on how you might go about putting the image on your site. This is the question I'm referencing.

In particular, this answer shows a really good example of the method you'll need to employ.

Basically, because you're getting the image as an array of bytes from the database (as in, the actual image it self, not a reference to the image), you'll need to use a slightly different method than an <asp:Image/> tag using the ImgUrl attribute.

In summation, you'll do something like this (reference from the other answer):

Create a regular HTML img element like so:

<img runat="server" id="image"  />

And in code behind do this:

image.src="data:image/png;base64,"+Convert.ToBase64String(imageBytes);

Where imageBytes is a byte[]

Community
  • 1
  • 1
brazilianldsjaguar
  • 1,409
  • 1
  • 20
  • 45
  • Can you show me how to do this by modifying my code `` – Moiz Jul 07 '14 at 17:31
  • I didn't know that what "And in code behind do this:" means I place `` In repeater but I am getting an error saying "element img is missing required attriute src" – Moiz Jul 07 '14 at 17:59
  • So I think your situation is a bit more complex. Within a handler you're calling a database and populating a repeater with a data table. This answer is catered towards a 'one-off', outside of a `DataTable` context. – brazilianldsjaguar Jul 07 '14 at 18:22