Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

global.asax with Connection String/error handling

Posted by Praveen Kumar on February 23, 2008

Example of global.asax with Connection String 

<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.Odbc” %>
<script language=”C#” runat=”server”>
    void Application_Start()
    {
        //Connection string for SQL Server with trusted connection
        Application.Add (“DB_CONNECTION_STRING”,”DRIVER={SQL Server};SERVER=ServerName;DATABASE=DataBaseName;Trusted_connection=yes;”);
    }
    void Session_Start()
    {
        //open connection to database
        OdbcConnection  cnConnection = new OdbcConnection (Application.Get(“DB_CONNECTION_STRING”).ToString());
        cnConnection.Open();
        Session.Add (“DB_CONNECTION”, cnConnection );
    }
    void Session_End()
    {
        //a graceful disconnect
        OdbcConnection cnConnection = (OdbcConnection) Session[“DB_CONNECTION”];
        cnConnection.Close ();
    }
</script>

 Example of global.asax with Connection String with Error Handling:

<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.Odbc” %>
<script language=”C#” runat=”server”>
    void Application_Start()
    {
        //Connection string for SQL Server with trusted connection
        Application.Add (“DB_CONNECTION_STRING”,”DRIVER={SQL Server};SERVER=ServerName;DATABASE=DataBaseName;Trusted_connection=yes;”);
    }
    void Session_Start()
    {
        //open connection to database
        try
        {  
            OdbcConnection  cnConnection = new OdbcConnection (Application.Get(“DB_CONNECTION_STRING”).ToString());
            cnConnection.Open();
            Session.Add (“DB_CONNECTION”, cnConnection );
        }
        catch(Exception err)
        {
            Response.Write(“Error: ” + err.ToString () + “<BR>”);
        }
    }
    void Session_End()
    {
        //a graceful disconnect
        OdbcConnection cnConnection = (OdbcConnection) Session[“DB_CONNECTION”];
        cnConnection.Close ();    }
</script>

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
%d bloggers like this: