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

7
  • Can you explain what you've already tried to do? And additional comments on how it didn't work? Commented Jul 7, 2014 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 <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ID")%>'/> 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 Commented Jul 7, 2014 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! Commented Jul 7, 2014 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? Commented Jul 7, 2014 at 17:13
  • 1
    Actually, take a look at this similar question here: stackoverflow.com/q/7390983/1245766 Commented Jul 7, 2014 at 17:16

1 Answer 1

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[]

Sign up to request clarification or add additional context in comments.

3 Comments

Can you show me how to do this by modifying my code <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ID")%>'/>
I didn't know that what "And in code behind do this:" means I place <img runat="server" id="image" /> In repeater but I am getting an error saying "element img is missing required attriute src"
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.