2 Replies - 3600 Views - Last Post: 04 April 2013 - 08:03 AM Rate Topic: -----

#1 mitkram  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-March 13

Unable to display image file from my database into the image control u

Posted 18 March 2013 - 02:28 PM

Hi everyone!,I need help in displaying my image file stored from my database into the
image control. I've had read some articles and watch video tutorial which there were perfectly
working and I followed it but I wasn't able to make it work. The image control just displayed blank.
I used Generic Handler to retrieve the image file from my database based on the articles and tutorials
I've got.I'll show you below my codes and please help me point out what did I miss?

Thanks for any help...here are my codes below:

Generic Handler:

<%@ WebHandler Language="C#" Class="ShowImage" %>

using System;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class ShowImage : IHttpHandler 
{

    public void ProcessRequest(HttpContext context)
    {

if (context.Request.QueryString["ID"] == null) return;
             //string connStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();
             string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Coldwind.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
             string ID = context.Request.QueryString["ID"];


             using (SqlConnection conn = new SqlConnection(connStr))
             {
                 using (SqlCommand cmd = new SqlCommand("Select Photos From ProfileTab Where ID =" + ID, conn))
                 {
                    
                     //cmd.Parameters.Add(new SqlParameter("@ID",ID));
                     conn.Open();
                     using (SqlDataReader reader = cmd.ExecuteReader(Commandbehavior.CloseConnection))
                     {
                         reader.Read();
                         context.Response.ContentType = "image/jpeg";
                         context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("Photos")]);
                        // byte[] imgData = (byte[])reader["Photos"];
                        // context.Response.BinaryWrite(imgData);
                         reader.Close();
                     }

                 }
             }
         } 


public bool IsReusable
    {
        get
        {
            return true;
        }
    }

}


Asp.Net Code for the Image control:

<asp:Image ID="Image1" runat="server" ImageUrl='<%# "ShowImage.ashx?ID=" + Eval("ID") %>' Height="128px" />

Is This A Good Question/Topic? 0
  • +

Replies To: Unable to display image file from my database into the image control u

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Re: Unable to display image file from my database into the image control u

Posted 20 March 2013 - 02:36 PM

Is it not an option to store your images elsewhere and just store paths to the images in the DB?
Was This Post Helpful? 0
  • +
  • -

#3 merlin'sbeard  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 20
  • Joined: 27-December 12

Re: Unable to display image file from my database into the image control u

Posted 04 April 2013 - 08:03 AM

Had the same problem, finally solved it by first converting the image from binary, assinging it a file name and saving it directly to a folder on my website and setting the image url to the file path of the image.

Note: After saving the image to the folder on your website do not expect the image to show when you double click the folder, the folder always appears empty, if you want to see if the image saved, navigate to your website folder on your drive(not in visual studio) and check it out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1