You must Sign In to post a response.
  • Category: ASP.NET

    Images display binary stored image on a datalist from sql server table

    Hi,

    I am having one binary table column in my database table where i am storing the images in binary format.

    Now i want to display the images on the page in a datalist.

    Please reply if anyone is having answer for this


    Thanks in advance
  • #324140
    Hi,
    retriving images from database like this

    <asp:DataList ID="DataList1" runat="server">
    <ItemTemplate>
    <asp:Image ID="image1" runat="server" ImageUrl='<#"GridHandler.ashx?id=" + Eval("Id")%>' Width="80" Height="77" />
    </ItemTemplate>
    </asp:DataList>



    ******** image_grid.aspx.cs*********


    protected void Page_Load(object sender, EventArgs e)
    {
    DataList1.DataSource = FetchAllImagesInfo();
    DataList1.DataBind();
    }
    private DataTable FetchAllImagesInfo()
    {
    string sql = "Select * from image";
    SqlDataAdapter da = new SqlDataAdapter(sql, cn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
    }



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

    using System;
    using System.Web;
    using System.Data;
    using System.Web.UI;
    using System.Data.SqlClient;


    public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["mailingConnectionString"].ToString());
    cn.Open();
    string sql = "Select image1 from image where Id=@Id";
    SqlCommand cmd = new SqlCommand(sql, cn);
    cmd.Parameters.Add("@Id", SqlDbType.Int).Value = context.Request.QueryString["id"];
    cmd.Prepare();
    SqlDataReader dr = cmd.ExecuteReader();
    dr.Read();
    context.Response.BinaryWrite((byte[])dr["image1"]);
    dr.Close();
    cn.Close();




    }

    public bool IsReusable {
    get {
    return false;
    }
    }

    }


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.