Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi all

I wrote the below code in handler for storing and fetching image from the database

Handler.ashx

C#
<%@ WebHandler Language="C#" Class="ShowImage" %>
 
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)
    {
 string conn = ConfigurationManager.ConnectionStrings  ["EmployeeConnString"].ConnectionString;
        SqlConnection connection = new SqlConnection(conn);
        string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID";
        SqlCommand cmd = new SqlCommand(sql,connection);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", empno);
        connection.Open();
        object img = cmd.ExecuteScalar();
        try
        {
            return new MemoryStream((byte[])img);
        }
        catch
        {
            return null;
        }
        finally
        {
            connection.Close();
        }
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
 
 
}


storing the image is working properly but retrieving is not working..

Please tell me how to retrieve

Thank you
Posted

public void DisplayEmployee()
{
//try
//{
dtObj.Clear();
flowLayoutPanel1.Controls.Clear();
string s = CmbShiftdetails.Text;
string a = CmbDept.Text;
string sql_displayemp = string.Format("SELECT DepartmentMaster.DepartmentId, DepartmentMaster.DepartmentName, EmployeeMaster.EmployeeId, EmployeeMaster.EmployeeCode, EmployeeMaster.EmployeeName, EmployeeMaster.Address, EmployeeMaster.PhoneNo, EmployeeMaster.Emailid, EmployeeMaster.Designation, EmployeeMaster.Photo, ShiftMaster.ShiftId, ShiftMaster.ShiftName FROM DepartmentMaster INNER JOIN EmployeeMaster ON DepartmentMaster.DepartmentId = EmployeeMaster.DepartmentId INNER JOIN ShiftMaster ON EmployeeMaster.ShiftId = ShiftMaster.ShiftId where ShiftMaster.ShiftName= '{0}' and DepartmentMaster.DepartmentName='{1}'", CmbShiftdetails.Text, CmbDept.Text);

dtObj = obj.NonTransaction(sql_displayemp);
if (dtObj.Rows.Count > 0)
{
TableLayoutPanel tlpobj = new TableLayoutPanel();
tlpobj.Name = "Emp";
tlpobj.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
tlpobj.AutoSize = true;

string[] headers = { "EmployeeCode", "EmployeeName", "Address", "PhoneNo", "Emailid", "Designation", "Photo", "Edit", "Delete" };
for (int i = 0; i < headers.Length; i++)
{
Label lbl = new Label();
lbl.Text = headers[i];
tlpobj.Controls.Add(lbl, i, 0);
}
for (int i = 0; i < dtObj.Rows.Count; i++)
{
Button btnedit = new Button();
btnedit.Text = "Edit";
btnedit.Name = "e_" + dtObj.Rows[i]["EmployeeId"].ToString();
btnedit.Click += new EventHandler(btnedit_Click);
btnedit.Width = 60;

Button btndelete = new Button();
btndelete.Text = "Delete";
btndelete.Name = "d_" + dtObj.Rows[i]["EmployeeId"].ToString();
btndelete.Click += new EventHandler(btndelete_Click);
btndelete.Width = 60;

Label lblempid = new Label();
lblempid.Text = dtObj.Rows[i]["EmployeeId"].ToString();
lblempid.Name = "lblempid" + dtObj.Rows[i]["EmployeeId"].ToString();
lblempid.Visible = false;

Label lblempcode = new Label();
lblempcode.Text = dtObj.Rows[i]["EmployeeCode"].ToString();
lblempcode.Name = "lblempcode" + dtObj.Rows[i]["EmployeeId"].ToString();

Label lblempname = new Label();
lblempname.Text = dtObj.Rows[i]["EmployeeName"].ToString();
lblempname.Name = "lblempname" + dtObj.Rows[i]["EmployeeId"].ToString();

Label lbladdress = new Label();
lbladdress.Text = dtObj.Rows[i]["Address"].ToString();
lbladdress.Name = "lbladdress" + dtObj.Rows[i]["EmployeeId"].ToString();

Label lblphone = new Label();
lblphone.Text = dtObj.Rows[i]["PhoneNo"].ToString();
lblphone.Name = "lblphone" + dtObj.Rows[i]["EmployeeId"].ToString();

Label lblemail = new Label();
lblemail.Text = dtObj.Rows[i]["Emailid"].ToString();
lblemail.Name = "lblemail" + dtObj.Rows[i]["EmployeeId"].ToString();

Label lbldesignation = new Label();
lbldesignation.Text = dtObj.Rows[i]["Designation"].ToString();
lbldesignation.Name = "lbldesignation" + dtObj.Rows[i]["EmployeeId"].ToString();


PictureBox pbemp = new PictureBox();
pbemp.SizeMode = PictureBoxSizeMode.StretchImage;
try
{

pbemp.Image = Image.FromStream(new MemoryStream(((byte[])dtObj.Rows[i]["Photo"])));
}
catch { }
pbemp.Name = "pbemp" + dtObj.Rows[i]["EmployeeId"].ToString();


tlpobj.Controls.Add(lblempcode, 0, i + 1);
tlpobj.Controls.Add(lblempname, 1, i + 1);
tlpobj.Controls.Add(lbladdress, 2, i + 1);
tlpobj.Controls.Add(lblphone, 3, i + 1);
tlpobj.Controls.Add(lblemail, 4, i + 1);
tlpobj.Controls.Add(lbldesignation, 5, i + 1);
//tlpobj.Controls.Add(lblemail, 4, i + 1);
tlpobj.Controls.Add(pbemp, 6, i + 1);
//tlpobj.Controls.Add(lbltagid, 6, i + 1);
// tlpobj.Controls.Add(lblshiftid, 7, i + 1);
tlpobj.Controls.Add(btnedit, 7, i + 1);
tlpobj.Controls.Add(btndelete, 8, i + 1);
}

flowLayoutPanel1.Controls.Add(tlpobj);
flowLayoutPanel1.Refresh();

}


For retriving this code helps you
 
Share this answer
 
cmd.ExecuteScalar() might be the problem.
Have you tried to fetch the image using cmd.ExecuteReader()?
 
Share this answer
 
cmd.ExecuteScalar() might be the problem.
Have you tried to fetch the image using cmd.ExecuteReader()?

Are all images is jpeg format?

What is the exact error you get? "Not working" is not really informative :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900