Pruthvi Patel

Pruthvi Patel

  • NA
  • 129
  • 23.5k

Retrieve Images from the database(Images datatype:image)

Mar 15 2016 1:02 PM
I have stored image in the sql and i  want to retrieve all the images in datalist. My code is like this.
Handler.ashx.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace project007
{
/// <summary>
/// Summary description for Handler5
/// </summary>
public class Handler5 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
// string diseasename = context.Request.QueryString["diseasename"];
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
con.Open();
SqlCommand command = new SqlCommand("select image from disease_details ", con);
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
context.Response.BinaryWrite((byte[])dr["image"]);
}
con.Close();
context.Response.End();
}
catch (Exception ex)
{
ex.ToString();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
 
 
 Webform.aspx:
 
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="diseaseid.aspx.cs" Inherits="project007.diseaseid" %>
<asp:Content ID="Content1" ContentPlaceHolderID="StyleSection" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentSection" runat="server">
<br/><br/><br/><br/><br/>
<div class="jumbotron">
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate >
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler5.ashx" %>'/>
</ItemTemplate>
</asp:DataList>
</div>
<div id="ImgEnlarge"></div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ScriptSection" runat="server">
</asp:Content>
 
Codebehind
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace project007
{
public partial class diseaseid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ToString());
SqlCommand command = new SqlCommand("SELECT image from disease_details ", con);
SqlDataAdapter ada = new SqlDataAdapter(command);
DataTable dt = new DataTable();
ada.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
}
 
 
 But when i run this project it will fetch the same image for as many time as the no of  row in the database.
Please Help Me for this