0

I have accomplished inserting an image into the Oracle database. Now I am trying to display it.

My handler code is :

    public void ProcessRequest(HttpContext context)
    {
        OracleDataReader dr = null;
        OracleCommand cmd = null;
        OracleConnection conn = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVER_NAME=XE)));User Id=sakthi_studdb;Password=sakthi;");
        try
        {
            cmd = new OracleCommand
          ("select IMAGE from IMAGETBL where ID=" +
          context.Request.QueryString["imgid"], conn);
            conn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                context.Response.ContentType = "image/jpg";
                context.Response.BinaryWrite((byte[])dr["IMAGE"]);
            }
            if (dr != null)
                dr.Close();
        }
        finally
        {
            if (conn != null)
                conn.Close();
        }
    }

And my aspx image control is :

    <div>
This is the student image requested:

    <asp:Image ID="picone" ImageUrl="~/Handler1.ashx?imgid=299" runat="server" />
</div>

When the run the aspx, I don't find any errors. But the image is not displaying. You can find the screen shot of the output I get below.

enter image description here

I want to know what is wrong.

     public void ProcessRequest(HttpContext context)
    {
        OracleConnection conn = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVER_NAME=XE)));User Id=sakthi_studdb;Password=sakthi;");
        try
        {
            long imageId = Convert.ToInt64(context.Request.QueryString["imgid"]);

            using (OracleCommand cmd = new OracleCommand("select IMAGE from IMAGETBL where ID=:ID", conn))
            {
                cmd.Parameters.Add(":ID", OracleDbType.Int64).Value = imageId;
                conn.Open();
                context.Response.ContentType = "image/gif";
                context.Response.BinaryWrite((byte[])cmd.ExecuteScalar());
            }                    

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

Even this din't work

6
  • 1
    stackoverflow.com/questions/698912/… Commented Apr 4, 2013 at 5:53
  • 1
    You should always use parameterized queries.This kind of codes are open for an SQL Injection attacks. Commented Apr 4, 2013 at 5:54
  • put a break point in your handler and see did it reach there? Commented Apr 4, 2013 at 5:59
  • I tried adding a break point. It is fetching the handler code. This means it reaches there. Commented Apr 4, 2013 at 6:21
  • and you are getting the value from the database? Commented Apr 4, 2013 at 6:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.