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

public class ShowImage : IHttpHandler
{
    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";
       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)
    {

        SqlConnection con = ConnectionManager.GetConnection();
        string sql = "SELECT emp_pic FROM employee WHERE emp_id = @ID";
        SqlCommand cmd = new SqlCommand(sql,con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", empno);
        con.Open();

        object img = cmd.ExecuteScalar();

        // how to verify if data is not available in database

        try
        {
            return new MemoryStream((byte[])img);
        }
        catch
        {
            return null;
        }
        finally
        {
            con.Close();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }


}

what exactly do you mean by not available?

you could try something like

if (img == null || img == DBNull.Value) { // no data returned

If you have any sort of control over the database, maybe putting images in a different table and having a default image if the user has/doesn't have an image uploaded is a good idea. You can link to it with a foreign key to link employees with their images and have a generic image that reads "Image Not Available" that you link no-image employees to. This will solve a lot of the client-side logic that you need to write and will save ever having to store duplicate images (just link them to the same image rather than store seperate images). Just my two cents.

As for your actual question - what Hearth said will work just fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.