Online Source Code Library -ASP, ASP.NET, VB, VB.Net, Microsoft® Access, Javascript, VBScript, Website Design, Web Applications, Custom Programming Services Available. ASP, ASP.NET, VB, VB.Net,
Microsoft® Access, Javascript, VBScript,
Website Design, Web Applications
Shopping Cart (0) Items $0.00


Website Stats to Date

Pages Viewed 6,975,154
Visitors 1,668,084
Widgets Viewed 4,500,691
Since January 1/2003
Active Users: 13

PayPal: Secure online payments!
Widget #CS0042

Create a Session Database Connection through code (ASP.NET)

Create a Session Database Connection through code (ASP.NET)

Send Link


Currency Conv.
  Widget Info  
the source code below will connect to an Access Database, to change to SQL Server make the appropriate changes to the Connection String
Create a an xml file named datasource.xml, and save it to the bin directory of your web application
Use the following XML as a template
Substitute your application name and database file name below
Make the following changes to your Global.asax.vb file
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started 
Session("DBCon") = DBCon()
End Sub
Function DBCon() As System.Data.OleDb.OleDbConnection
'Load datasource path from XML File
Dim xml As New System.Xml.XmlDocument()
xml.Load(Server.MapPath(".") & "\bin\datasource.xml")
'Pass value to Connection Path
Dim Path As String = xml.SelectSingleNode("/DataSource/Path[1]").InnerText()
'Build Connection String
Dim strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Password='';User ID=Admin;Data Source=" & Path & ";Mode=Share Deny None"
'Open Database
DBCon = New System.Data.OleDb.OleDbConnection(strConnectionString)

End Try
End Function
Function CloseDBCon(ByVal m_DBCon As System.Data.OleDb.OleDbConnection) As Boolean
End Function
To use the Connection in a web form's code behind file
Dim DBCon as new System.Data.OleDb.OleDbConnection()
DBCon = Session("DBCon")
WARNING: If at anytime you close the connection to your database it must be re-opened before it can be used
Format: HTML Article
Online Document
Licencing Terms: Reserved Copyright
Copyright 2002
Reproduction or duplication of this article without the authors permission is prohibited and protected under the copyright laws of Canada.
This source code is provided to you "as is" without warranty
Copyright 2002-2006 Comrie Software Solutions
 Related Widgets

Add Sounds to button events. ASP.NET
Databound Grid with Subtotals (ASP.NET)
DateRange.NET Class
Dynamic Drop Down Web Menu
Hierarchal Data Display using a DataList and DataGrid (ASP.NET)
Hit Counter Class with Database (ASP.NET)
PayPal UserControl for ASP.NET
Storing a DataSet in a Session Variable (ASP.NET)
Using a DataGrid as a bar graph (ASP.NET)

ShoppingCart Empty
Services Contact Info Legal Info
Designated trademarks and brand names are the property of their respective owners.

By accessing this website it is assumed that you agree to the TERMS OF USE.

Send questions or comments regarding this website to  Comrie Software Solutions.

Click here for a complete list of available Widgets.