Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to retrieve image from Sql DB in ASP.NET web page and display it.
I already did the Sql proc that retrieve the image but I don't know how to display it in asp.net.
This is the code I write:
public List<Image> select_Image(string Product_Name)
    {
        //Connection.Close();
        Connection.Open();
        SqlCommand select_Product_Image = new SqlCommand("select_Product_Image", Connection);
        select_Product_Image.CommandType = CommandType.StoredProcedure;
        select_Product_Image.Parameters.Add("@Product_Name", SqlDbType.NVarChar);
        select_Product_Image.Parameters["@Product_Name"].Value = Product_Name;
        select_Product_Image.ExecuteNonQuery();
        Connection.Close();
        DataTable dt1 = new DataTable();
        SqlDataAdapter ad = new SqlDataAdapter(select_Product_Image);
        ad.Fill(dt1);
        //for (int i = 0; i < 3; i++)
        {
            MemoryStream ms = new MemoryStream((byte[])dt1.Rows[0][0]);
            Image T ;
            Response.ContentType = "image/gif";
            Response.BinaryWrite(ms);
            //T.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            //Images.Add(T);
            //Temp_id.Add((int)(dt1.Rows[i][1]));
        }
        return Images;
    }

Is it right and what I can do to diplay image?

[edit]
Code block added to improve formatting.
Converted form all lower case to proper capitalisation. (All lower case is considered "childish", just as all upper case is considered "shouting".
Punctuation added.
Spelling improved and vowels added where required.
Some minor grammar.
OriginalGriff
[/edit]
Posted
Updated 23-Dec-10 22:05pm
v2

If you need to know more check this, it help you
Image Handling In ASP.NET[^]
 
Share this answer
 
protected void display_Click(object sender, EventArgs e)
{

string img_id = "1"; //Request.QueryString["img_pk"];

SqlConnection con = new SqlConnection(@"Data Source=...\SQLEXPRESS;Initial Catalog=....;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select img_contenttype,img_data from image where img_pk=" + img_id, con);
con.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
Response.ContentType = dr[0].ToString();
//Response.BinaryWrite((byte[])dr[1]);
string strURL =" ~/Default.aspx?img_pk=1";
//Image imgload = new Image();
Image1.ImageUrl = strURL;
}
con.Close();

}

for me its not displaying image in image control ..check it pls..
 
Share this answer
 
v2
can you explain what you want to do? I mean you want to show image on page or in image control. I want to show image in control then you have to go for HttpHandler Class.
Try following Code
Add HttpHandler Class
write this code
C#
<%@ WebHandler Language="C#" Class="Handler" %>

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

public class Handler : IHttpHandler
{
    
    public void ProcessRequest(HttpContext context)
    {
        string _productname;
        if (context.Request.QueryString["_productname"] != null)
           _productname=(context.Request.QueryString["_productname"]).ToString();
        else
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/gif";
        Stream strm = ShowEmpImage(_productname);
        byte[] buffer = new byte[2525];
        int byteSeq = strm.Read(buffer, 0, 2525);

        while (byteSeq > 0)
        {
            context.Response.OutputStream.Write(buffer, 0, byteSeq);
            byteSeq = strm.Read(buffer, 0, 2525);
        }
        //context.Response.BinaryWrite(buffer);
    }

    public Stream ShowEmpImage(string _productname)
    {
        string conn = ConfigurationManager.ConnectionStrings["sqlcn"].ConnectionString;
        SqlConnection connection = new SqlConnection(conn);
        string sql = "Ur Sp";
        SqlCommand cmd = new SqlCommand(sql, connection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Product_Name", _productname);
        connection.Open();
        object img = cmd.ExecuteScalar();
        try
        {
            return new MemoryStream((byte[])img);
        }
        catch
        {
            return null;
        }
        finally
        {
            connection.Close();
        }
    }
    

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }


}

write following code in aspx.cs file
C#
Image imgload = new Image();
                imgload.ImageUrl = "~/Handler.ashx?_productname=" + _productname;

if any clarification need feel free to ask
 
Share this answer
 
v4
Comments
thatraja 24-Dec-10 4:59am    
Hi use Code block to format your code & please avoid txtspeak(like 'u' instead of 'you').
TejuKrishna 24-Dec-10 5:15am    
i will take care onwords
Hiren solanki 24-Dec-10 7:39am    
Comment by eman88:
thanks for your repaly
what i want to do is to give the program product name and it will display the image for this product what ever if will display it in control or if there is another way to dispaly

i try this code but it does not work
i don't know what is the rong
TejuKrishna 24-Dec-10 7:46am    
See the execution of Handler Class by putting beakpoint. Because this will happen cause of the byte declare in my code dose not match with your image byte. Take one look to that if this is the cause then simply update byte size try it.
eman88 24-Dec-10 7:57am    
this code works with me just when i put img control and retrive the image on it ,,, what iwant really is to create this control at runtime ,,,,i will use it in web site and user can choose more than product ,i wnat to display the images for all products he choose

thanks again :) :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900