CodeWidgets.com: Online Source Code Library: C#, ASP, ASP.NET, VB, VB.NET, VB Script, Microsoft Access, Excel, JavaScript, J Script, XML, VBScript, Website Design, DevExpress Components, Reporting, Application and Website Templates.

Widget #CS0042

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

Free

VB.NET / ASP.NET
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
<DataSource> 
<Path>C:\Inetpub\wwwroot\
appname\databasename.mdb</Path>
</DataSource>
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
Try
'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)
DBCon.Open()
Catch

End Try
End Function
Function CloseDBCon(ByVal m_DBCon As System.Data.OleDb.OleDbConnection) As Boolean
m_DBCon.Close()
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

Last Updated: 1/12/2003


Related Widgets 
Price 
# 
CS0008 DateRange.NET Class (Visual Basic.Net)$10.00Add To Cart
CS0197 Storing a DataSet in a Session Variable (ASP.NET)$0.00Add To Cart

Recently Viewed Widgets 
Price 
# 
No data to display

Collapse/Expand
Format

HTML Article

Online Document


Collapse/Expand
License

License

Reserved Copyright

Copyright 2018 CodeWidgets.com
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
CLICK HERE FOR TERMS OF USE
Copyright 2000-2006 Comrie Software Solutions