[ Download Code ]
While this is not necessary I find it useful to separate my common functionality into it's own project. For this example I am using this utility class for my database handling. You could easily integrated thing such as sending emails, logging errors, and string functions to name a few.
In this class I have two methods and a constant string that I have defined for the database connection string. Once again I am taking advantage of the ability to define values in a single local and in the case where I define the database connection string this value is stored in the Web.config file, of which below is an example.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="OracleConnectionString"
value="User ID=userid;Password=password;Data Source=datasource" />
</appSettings>
</configuration>
Now to reference this connection string in you class simply use the System.Configuration namespace. Be sure to import your ODP.NET Oracle namespaces as well.
using System;
using System.Data;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Configuration;
using System.Collections;
namespace ASPAlliance.Utilities
{
/// <summary>
/// Summary description for OracleDatabaseHelper.
/// </summary>
public class OracleDatabaseHelper
{
// Read the connection strings from the configuration file
// Note: Modify User Id, Password, Data Source per your
// database setup this is contained with the Web.config
public static readonly string CONN_STRING =
ConfigurationSettings.AppSettings["OracleConnectionString"];
/// <summary>
/// Establishes your database connection
/// </summary>
/// <returns>a database connection instance</returns>
public OracleConnection openDatabaseConnection()
{
OracleConnection dbConn = new OracleConnection(CONN_STRING);
dbConn.Open();
return dbConn;
}
public void closeDatabaseConnection(OracleConnection dbConn)
{
// Check if connection is open or closed
if (dbConn != null)
{
if (dbConn.State == ConnectionState.Open)
{
dbConn.Close();
dbConn.Dispose();
}
}
}
}
}
Now when you are ready to instantiate this class an example would be as so.
ASPAlliance.Utilities.OracleDatabaseHelper dbHelper =
new ASPAlliance.Utilities.OracleDatabaseHelper();
In the code behind the web form you will notice when I have my database connection wrapped in a try/catch/finally statement. I will not go into the details of this rather if you are not familiar with try-catch-finally statement read Customizing Error Pages and C# Programmers Reference: try-catch-finally.
Example:
try
{
// Establish the database connection
dbConn = dbHelper.openDatabaseConnection();
// Execute the SQL Statement
cmd.ExecuteNonQuery();
}
catch(OracleException ex)
{
NotificationLabel.Text = ex.Message;
NotificationLabel.Visible = true;
}
finally
{
// Close the database connection
dbHelper.closeDatabaseConnection(dbConn);
}
At this point you should be able to execute this application in your web browser and store an image and the other relevant information in the database and in this case the database is an Oracle instance. From here the next step is to create another web form which will display the stored images.