Read and Write BLOB Data to a Database Table with ODP.NET
page 7 of 7
by Steven Swafford
Average Rating: 
Views (Total / Last 10 Days): 68369/ 165

Display the Selected Image

[ Download Code ]

This is where the big payoff comes in. Once a user selects an image they wish to view you will need to accomplish the following steps.

Create yet another web form only this time the only control you need to add is a Label and again the Label is used form displaying any errors that may arise otherwise we will display the image. Here is the code for the web form.

<%@ Page language="c#" Codebehind="Article570_2.aspx.cs" 
AutoEventWireup="false" Inherits="ASPAllianceArticles.Article570_2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
      <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
      <meta name="CODE_LANGUAGE" Content="C#">
      <meta name="vs_defaultClientScript" content="JavaScript">
      <meta name="vs_targetSchema" 
      <form id="Form1" method="post" runat="server">
            <asp:Label id="NotificationLabel" runat="server" Visible="False"></asp:Label></P>

The requirement here is the photo id which is used to pull the correct record from the database and display the image using the BinaryWrite method.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

// Application Import(s)
using ASPAlliance.Utilities;

namespace ASPAllianceArticles
 /// <summary>
 /// Summary description for Article570_2.
 /// </summary>
 public class Article570_2 : System.Web.UI.Page
      protected System.Web.UI.WebControls.Label NotificationLabel;
  private void Page_Load(object sender, System.EventArgs e)
         string imageId = Request.QueryString["imageId"];
         OracleConnection dbConn = null;
         ASPAlliance.Utilities.OracleDatabaseHelper dbHelper =
            new ASPAlliance.Utilities.OracleDatabaseHelper();

            // Establish the database connection
            dbConn = dbHelper.openDatabaseConnection();

            // define the sql to perform the database insert
            string sqlStmt = "select photo from smstestblob where id = " + imageId;


            // Establish a new OracleCommand
            OracleCommand cmd = new OracleCommand();

            // Set command to create your SQL statement
            cmd.CommandText = sqlStmt;

            // Set the OracleCommand to your database connection
            cmd.Connection = dbConn;
            // Set the command type to text
            cmd.CommandType = CommandType.Text;

            // Execute the SQL Statement
            OracleDataReader oraReader = cmd.ExecuteReader();

            if ( oraReader.Read())
               Response.BinaryWrite( (byte[]) oraReader["photo"] );
            //ImageDataGrid.DataSource = oraReader;
         catch(OracleException ex)
            NotificationLabel.Text = ex.Message;
            NotificationLabel.Visible = true;
            // Close the database connection

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
         this.Load += new System.EventHandler(this.Page_Load);


At this point when you clicked view image on the previous webform you will have the image presented to your user via their browser.

As this article now comes to a close, you should successfully be able to establish the database schema to handle a Blob, build the web form for the end user to utilize, and finally see the usefulness of the separation of common functionality whether you utilize a class or the Web.config file. Good luck and I would relish the idea of hearing any success stories born from this article so be sure to post you comments.

Feel free to discuss this article at my Blog.

View Entire Article

User Comments

Title: Read/Write BLOB data when file > 32K   
Name: Roger Rowe
Date: 2005-03-07 1:03:00 PM
Go see this article:;en-us;322796
This shows you how to insert larger data - your version will die for data larger than 32K - a PLSQL limitation).

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2019  |  Page Processed at 2019-06-19 7:34:34 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search