<%@ WebHandler Language="C#" Class="ShowImage" %> using System; using System.Web; using System.IO; using System.Data; using System.Data.SqlClient; using System.Configuration; public class ShowImage : IHttpHandler { SqlConnection con; public void ProcessRequest (HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); ////context.Response.ContentType = "text/plain"; // //context.Response.Write("Hello World"); //Int32 empno; string empno; if (context.Request.QueryString["roll_number"] != null) //empno = Convert.ToInt32(context.Request.QueryString["id"]); empno = Convert.ToString(context.Request.QueryString["roll_number"]); else throw new ArgumentException("No parameter specified"); context.Response.ContentType = "image/jpeg"; Stream strm = ShowEmpImage(empno); if (strm == null) { } else { byte[] buffer = new byte[4096]; int byteSeq = strm.Read(buffer, 0, 4096); while (byteSeq > 0) { context.Response.OutputStream.Write(buffer, 0, byteSeq); byteSeq = strm.Read(buffer, 0, 4096); } } //context.Response.BinaryWrite(buffer); } public Stream ShowEmpImage(string empno) { con = new SqlConnection(ConfigurationManager.ConnectionStrings["regis_dbConnectionString"].ConnectionString); //string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString; //SqlConnection connection = new SqlConnection(conn); //string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID"; string sql = "SELECT image_data FROM image_master WHERE roll_number = @ID"; //SqlCommand cmd = new SqlCommand(sql, connection); SqlCommand cmd = new SqlCommand(sql, con); cmd.CommandType = CommandType.Text; //cmd.Parameters.AddWithValue("@ID", empno); cmd.Parameters.AddWithValue("@ID", empno); //connection.Open(); con.Open(); object img = cmd.ExecuteScalar(); try { return new MemoryStream((byte[])img); } catch { return null; } finally { //connection.Close(); con.Close(); } } public bool IsReusable { get { return false; } } }