Tuesday, 19 July 2011

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;

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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

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 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>&nbsp;
<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 are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

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.AspNetTutorials.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!

No comments:

Post a Comment