?
Solved

DISPLAY AN IMAGE FROM DATABASE IN ASPX USING STORED PROCEDURE

Posted on 2006-05-19
3
Medium Priority
?
261 Views
Last Modified: 2006-11-18
Hello! I wanted to display an image from my database in a datalist. I have my code below:

<asp:datalist id="MyList" Width="720px" runat="server" RepeatColumns="5" RepeatDirection="Horizontal"
HorizontalAlign="Center">
<ItemTemplate>
      <a href='ProductDetails.aspx?productID=<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' target="_self"><asp:Image width="100" height="120" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ProductImageThumb") %>' Runat="server"/></a>
</ItemTemplate>
</asp:datalist>

When I run the application, the image doesn't show.
When I rightclick on the page and view page source, the image tag is like this:
<img src="System.Byte[]" alt="" border="0" style="height:120px;width:100px;" />

The data that I want to retrieve from my database is of image data type.
I know I'm missing a funtion in the CodeBehind also aside from the store proc.

Anyone here knows how to do this? Retrieve data of image data type from a database in aspx using store procedure??

Your assistance would be greatly appreciated. Thanks in advance.
0
Comment
Question by:JaimeJegonia
  • 2
3 Comments
 
LVL 19

Expert Comment

by:Fahad Mukhtar
ID: 16716488
private void Page_Load(object sender, System.EventArgs e)
{
    // Put user code to initialize the page here
    MemoryStream stream = new MemoryStream ();
    SqlConnection connection = new
      SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
    try
    {
        connection.Open ();
        SqlCommand command = new
          SqlCommand ("select Picture from Image", connection);
        byte[] image = (byte[]) command.ExecuteScalar ();  
        stream.Write (image, 0, image.Length);
        Bitmap bitmap = new Bitmap (stream);
        Response.ContentType = "image/gif";
        bitmap.Save (Response.OutputStream, ImageFormat.Gif);
    }
    finally
    {
        connection.Close ();
        stream.Close ();
    }
}
0
 
LVL 19

Accepted Solution

by:
Fahad Mukhtar earned 2000 total points
ID: 16716489
0
 

Author Comment

by:JaimeJegonia
ID: 16722996

bitmap.Save (Response.OutputStream, ImageFormat.Gif); --> Where did ImageFormat variable come from?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question