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

Establish a Web Form that Utilizes a Datagrid to Display Information

[ Download Code ]

Previously you were instructed how to store images and relevant data in your database. Now you will need a method to allow your users to view records and then the ability to view these images.


The web form above has two controls, a DataGrid and a Label. The Label is used for display any errors that may arise. The following is the code for this web form.

<%@ Page language="c#" Codebehind="Article570_1.aspx.cs" 
AutoEventWireup="false" Inherits="ASPAllianceArticles.Article570_1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
      <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
      <meta content="C#" name="CODE_LANGUAGE">
      <meta content="JavaScript" name="vs_defaultClientScript">
      <meta content="" 
      <form id="Form1" method="post" runat="server">
            <asp:DataGrid id="ImageDataGrid" runat="server" 
            BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
               BackColor="White" CellPadding="4" AutoGenerateColumns="False">
               <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
               <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66">
               <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
               <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000">
                  <asp:HyperLinkColumn Text="id" Target="_blank" 
                  DataTextField="id" HeaderText="View Image" NavigateUrl="link.aspx" 
                  DataTextFormatString="View Image">
                     <HeaderStyle HorizontalAlign="Center" Width="20%"></HeaderStyle>
                  <asp:BoundColumn DataField="author" HeaderText="Author">
                     <HeaderStyle HorizontalAlign="Center" Width="40%"></HeaderStyle>
                  <asp:BoundColumn DataField="description" HeaderText="Description">
                     <HeaderStyle HorizontalAlign="Center" Width="40%"></HeaderStyle>
               <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC">
            <asp:Label id="NotificationLabel" runat="server" Visible="False"></asp:Label></P>

The next step will be to bind the data to the DataGrid. Once again take notice of the utility class that I previously spoke of.

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_1.
 /// </summary>
 public class Article570_1 : System.Web.UI.Page
      protected System.Web.UI.WebControls.DataGrid ImageDataGrid;
      protected System.Web.UI.WebControls.Label NotificationLabel;
  private void Page_Load(object sender, System.EventArgs e)
         OracleConnection dbConn = null;
   string sqlStmt = null;
         ASPAlliance.Utilities.OracleDatabaseHelper dbHelper =
            new ASPAlliance.Utilities.OracleDatabaseHelper();

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

            // define the sql to perform the database insert
            sqlStmt = "select,, t.description" +
               " from smstestblob t order by";

            // 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();
            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);


The end result when this application is executed in your browser will look similar to the following screenshot.

Now for the final step which will display the selected image.

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 2:02:13 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search