Click here to Skip to main content
15,922,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i want to show the image from database to image control. so that i searched in net and i found handler is the way to show image from database and i tried one code from net but its not working.
C#
>>>>handler code<<<<<<br mode="hold" />
public void ProcessRequest(HttpContext context)
    {
         Int32 empno;
       if (context.Request.QueryString["eid"] != 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 connection = new SqlConnection("........");

        string sql = "SELECT image FROM exp2 WHERE eid =@eid";
        SqlCommand cmd = new SqlCommand(sql,connection);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@eid", 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;
        }
    }

>>>>>page code<<<<

Image1.ImageUrl = "~/ShowImage.ashx?id=" + eid;

This is the code in this its showing error(The name 'eid' does not exist in the current context) in my page code..... i think i have to declare eid but i dont know where....... any one pl help me....
Posted
Updated 1-Jun-12 21:40pm
v2

1 solution

you change in your code as


C#
string sql = "SELECT image FROM exp2 WHERE eid ="+ empno;

SqlCommand cmd = new SqlCommand(sql,connection);
        cmd.CommandType = CommandType.Text;
        connection.Open();
        object img = cmd.ExecuteScalar();
 
Share this answer
 
Comments
dineshdena 2-Jun-12 3:52am    
no sir its showing error like same.... actually in that code eid value is get from empno... but i dnt where we giving that empno value????

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