Retrieve BLOB Field from Oracle DB Using ASP.NET

At this article, I will demonstrate How to

Read BLOB  field from Oracle using Generic Handler in ASP.NET.


What’s the Generic Handler in ASP.NET?

Some ASP.NET files are dynamic. They are generated with C# code or disk resources. These files do not require web forms. Instead, an ASHX generic handler is ideal.

It can return an image from a query string, write XML, or any other data.to know more about Generic Handler check this.


Generic Handler (ASHX) VS Web Forms (ASPX)

The WebForms (ASPX) can be used in

  • Simple Html Pages.
  • ASP.Net Custom Controls.
  • Simple Dynamic Pages.

The Handlers(ASHX) can be used with

  • Binary files.
  • Dynamic Image Views.
  • Performance critical web pages.
  • XML files.
  • Minimal Web Pages.

Steps:

Get BLOB  field from Oracle using Generic Handler in ASP.NET

Open Visual Studio > Open existing  ASP.NET Solution > Add tag within a page that should look like this

<img id="EmpImg" 
src="ShowImage.ashx?Account="
alt="" width="100px" height="100px" style="float:right"/>

Note:  The image source is a generic handler called ShowImage.ashx.

Create a Generic Handler In Visual Studio

  • Open your ASP.Net Web Site and go to File menu > New >Check Generic Handler.

Create a Generic Handler In Visual Studio

  • Rename the generic handler with an appropriate name. In my case, it is  ShowImage.
  • Click on add, the below-structured file will be shown.

Generic Handler In Visual Studio

Note: Handler is an interface that includes a function called “ProcessRequest” which will be used to execute the code that has been placed within it.

  • In Handler, add System.Data.OracleClient namespace

using System.Data.OracleClient; //Oracle client to can Work with Oracle Database

  • In “ProcessRequest”  function Add the following code to retrieve a BLOB field from OracleDB.

public void ProcessRequest(HttpContext context) {

string Account;

if (context.Request.QueryString["Account"] != null) {

Account = context.Request.QueryString["Account"].ToString();

OracleConnection con = new OracleConnection(OracleConn);

con.Open();

OracleCommand com = con.CreateCommand();

com.CommandText = "(SELECT pi.image img  FROM fnd_user fu,per_images pi WHERE fu.user_name = '" + Account.ToUpper() + "'   AND fu.employee_id = pi.parent_id)";

OracleDataReader img = com.ExecuteReader();

try {

img.Read();

context.Response.BinaryWrite((byte[])img[img.GetOrdinal("img")]); img.Close();

}

catch { }

finally { con.Close(); } } }

Read BLOB  field from Oracle using Generic Handler in ASP.NET

  • Finally, Make sure that tag source parameters are provided correctly.
  • Go back to run the Web Application that should now be working properly.

Retrieve BLOB  field from Oracle using Generic Handler in ASP.NET


See also
Advertisements

One thought on “Retrieve BLOB Field from Oracle DB Using ASP.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s