
Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
Get Connectionstring using ASP.NET 2.0 and VB.NET
This tutorial will show you how to get connectionstring from configuration file using ASP.NET 2.0 and VB.NET.
At first, please import the namespace from System.Configuration.
The System.Configuration namespace contains classes that represents a configuration file applicable to a particular computer, application, or resource.
| Imports System.Configuration; |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
In order to show the result of the sample, we will use the btn_GetConn_Click and GetConnectionString to perform the task. The sample code as following:
| Protected Sub btn_GetConn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_GetConn.Click
Me.lblMessage.Text = GetConnectionString(Me.tb_connName.Text.Trim()) End Sub
Private Function GetConnectionString(ByVal _connectionStringsName As String)
Dim config As System.Configuration.ConnectionStringSettingsCollection = System.Configuration.ConfigurationManager.ConnectionStrings For i As Integer = 1 To config.Count
If config(i).Name.Equals(_connectionStringsName, StringComparison.OrdinalIgnoreCase) Then
Return config(i).ToString() End If Next Return String.Empty End Function |
The front end SeekConnectionStringVB.aspx page looks something like this:
<div align="center"> <asp:Label ID="Label1" runat="server" Text="Connection Name:"></asp:Label> <asp:TextBox ID="tb_connName" runat="server"></asp:TextBox> <asp:Button ID="btn_GetConn" runat="server" Text="Get ConnectionString" OnClick="btn_GetConn_Click" /></div> <div align="center"> <asp:Label ID="lblMessage" runat="server" ForeColor="Red">ConnectionString1,ConnectionString2</asp:Label> </div> |
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
The flow for the code behind page is as follows
Partial Class _Default
Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' This tutorial is provided in part by Server Intellect Web Hosting Solutions http://www.serverintellect.com ' Visit http://www.DotNetTutorials.com for more ASP.NET Tutorials End Sub Protected Sub btn_GetConn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_GetConn.Click
Me.lblMessage.Text = GetConnectionString(Me.tb_connName.Text.Trim()) End Sub Private Function GetConnectionString(ByVal _connectionStringsName As String)
Dim config As System.Configuration.ConnectionStringSettingsCollection = System.Configuration.ConfigurationManager.ConnectionStrings For i As Integer = 1 To config.Count
If config(i).Name.Equals(_connectionStringsName, StringComparison.OrdinalIgnoreCase) Then
Return config(i).ToString() End If Next Return String.Empty End Function End Class |
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!