ashima kukreja

ashima kukreja

  • NA
  • 19
  • 13.8k

asp.net

Jun 21 2013 5:01 AM
hello ,


        i have problem while retrieving images from the database.i am saving 5 images in database an i want to show them in image box using hanler i am not able to retrive 5 images i am giiving the code plz rectify it.

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

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


public class Handler : IHttpHandler {

    SqlConnection cn;
    public void ProcessRequest(HttpContext context)
    {
        Int32 empno;
        if (context.Request.QueryString["id"] != null)
            empno = Convert.ToInt32(context.Request.QueryString["id"]);
        else
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/jpeg/jpg";
        Stream strm = ShowEmpImage(empno);
        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(int empno)
    {
        cn = new SqlConnection("Data Source=ADMIN-PC;initial catalog=YesGroup;Integrated Security=True");
        cn.Open();
        string sql = "SELECT Pictures FROM registration WHERE userid = @ID";
        SqlCommand cmd = new SqlCommand(sql, cn);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID",empno);

        object img = cmd.ExecuteScalar();  (Here is the problem how to make more object so that i can retrieve 5 images)
        try
        {
            return new MemoryStream((byte[])img);
        }
        catch
        {
            return null;
        }
        finally
        {
            cn.Close();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Answers (2)