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 SubPrivate Function GetConnectionString(ByVal _connectionStringsName As String) Dim config As System.Configuration.ConnectionStringSettingsCollection = System.Configuration.ConfigurationManager.ConnectionStrings End Function For i As Integer = 1 To config.Count If config(i).Name.Equals(_connectionStringsName, StringComparison.OrdinalIgnoreCase) Then NextReturn config(i).ToString() End IfReturn String.Empty |
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> <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 End Class 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 End Sub' Visit http://www.AspNetTutorials.com for more ASP.NET Tutorials 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 SubPrivate Function GetConnectionString(ByVal _connectionStringsName As String) Dim config As System.Configuration.ConnectionStringSettingsCollection = System.Configuration.ConfigurationManager.ConnectionStrings End FunctionFor i As Integer = 1 To config.Count If config(i).Name.Equals(_connectionStringsName, StringComparison.OrdinalIgnoreCase) Then NextReturn config(i).ToString() End IfReturn String.Empty |
No comments:
Post a Comment