Programming in almost language

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

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

  • Advertisements

Global Connection Strings

Posted by Praveen Kumar on March 8, 2008

Here i am going to describe how you can create a global connection for all pages.
as i have already describe in my web.config and global.asax(please refer it to know more) category
This is my web.config file where i may store number of connection.
Here i create two connetion with ODBC driver and SQL Server driver


    <add name=”mySqlConn” connectionString=”Driver={MySQL ODBC 5.1 Driver};Server=;Database=payroll_master;Uid=praveen;”/>
    <add name=”sqlConnString” connectionString=”Driver={SQL Server};Server=;database=mydatabase;Uid=sa;”/>
    <!–<add key=”myConn” value=”provider=MySQL ODBC 5.1 Driver;server=;database=payroll_master;User Id=praveen;”/>–>

and this is my seperate class file named ConnectionClass which use connectionstrings defined in web.config file


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;

public class ConnectionClass : System.Web.UI.Page
    OdbcConnection conn;
     public ConnectionClass()
   public OdbcConnection Connect_thru_config()
        string connString;
//If you want to create MySQL connection then you need to pass only mySqlConn
 //connString = System.Configuration.ConfigurationManager.ConnectionStrings[“mySqlConn”].ConnectionString;
        connString = System.Configuration.ConfigurationManager.ConnectionStrings[“sqlConnString”].ConnectionString;//for Sql
        OdbcConnection conwithfig = new OdbcConnection(connString);
        return conwithfig;

and this is the ConnectionDemo.aspx.cs page where i am calling Connect_thru_config()


            ConnectionClass conobj = new ConnectionClass();
            configcn = conobj.Connect_thru_config();
            DataSet ds = new DataSet();
            string sql = “select *from FK_TBLUSER”;
            OdbcDataAdapter da = new OdbcDataAdapter(sql, configcn);
            GridView1.DataSource = ds;
        } catch (Exception e1)     {       Page.Response.Write(“error” + e1);       }
        finally        {            configcn.Close();        }


This article describes that you may have number of connection in config file and call these connection strings in a seperate class file in a method, and you may use this class anywhere in your project. you do not need to write connection string in each page.
I hope this articles may help you. If you want to give any suggestion please you can leave your comment to improve this article.
your comments and suggestion will be most appreciable.


Leave a Reply

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

You are commenting using your 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 )


Connecting to %s

%d bloggers like this: