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 description for Handler5
///
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" %>
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
Replies
Know the answer? Post it — somebody with the same question will find it here.