0

I am working on asp.net with oracle database. I want to print the image of employee which is saved in an old table. I don't even the data type of image saved in the photo field of that table.

I used Image handlers to print the images from newly created table but when I query on old tables the images is not getting printed.

How do I know that is there any image saved in the table?

If there is an image why it's not getting printed?

I'll show you the code for image handler for both the table (NEW , OLD) Image from newly created table is printing very fine but what's the problem in old one.

Can any give me any suggestions ?

Here is my ImgHandler.ashx code ;

 public void ProcessRequest (HttpContext context)
    {
        OracleDataReader rdr = null;
        OracleConnection conn = Conn.getConn();
        OracleCommand cmd = null;
        string ImgType = context.Request.QueryString["typ"].ToString();
        try
        {
            if (ImgType=="edu")//this is working fine
            {
                cmd = new OracleCommand("select attachment pic from newtbl where lvldcp_code=" + context.Request.QueryString["dcp"] + "and emp_code="+context.Request.QueryString["emp"], conn);
            }
            else if (ImgType=="profile")
            {
                cmd = new OracleCommand("select photo pic from oldtbl where emp_code="+context.Request.QueryString["emp"], conn);
            }

            conn.Open();
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                context.Response.ContentType = "image/jpg";
                context.Response.BinaryWrite((byte[])rdr["pic"]);
            }
            if (rdr != null)
                rdr.Close();
        }
        catch (Exception ex)
        {

        }
        finally
        {
            if (conn != null)
                conn.Close();
        }


    }
DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
Alina Anjum
  • 1,178
  • 6
  • 30
  • 53

1 Answers1

0

If your queries are returning a blob field value then you could use the OracleBlob class.

public void ProcessRequest (HttpContext context)
{
    OracleDataReader rdr = null;
    OracleConnection conn = Conn.getConn();
    OracleCommand cmd = null;
    string ImgType = context.Request.QueryString["typ"].ToString();
    try
    {
        if (ImgType=="edu")//this is working fine
        {
            cmd = new OracleCommand("select attachment pic from newtbl where lvldcp_code=" + context.Request.QueryString["dcp"] + "and emp_code="+context.Request.QueryString["emp"], conn);
        }
        else if (ImgType=="profile")
        {
            cmd = new OracleCommand("select photo pic from oldtbl where emp_code="+context.Request.QueryString["emp"], conn);
        }

        Byte[] byteArray = null;
        OracleBlob blob;
        conn.Open();
        rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            blob = rdr.GetOracleBlob(0);
            byteArray = new Byte[blob.Length];
            int i = blob.Read(byteArray, 0, System.Convert.ToInt32(blob.Length));

            //clob.Length or i > 0 will show if there are bites in the clob or not
        }
        if (rdr != null)
            rdr.Close();

        context.Response.ContentType = "image/jpg";
        context.Response.BinaryWrite(byteArray);
    }
    catch (Exception ex)
    {

    }
    finally
    {
        if (conn != null)
            conn.Close();
    }

}
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69