

Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
Get Connectionstring using ASP.NET 2.0 and C#
This tutorial will show you how to get connectionstring from configuration file using ASP.NET 2.0 and C#.
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.
| using System.Configuration; |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
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:
public string GetConnectionString(string _connectionStringsName) { System.Configuration.ConnectionStringSettingsCollection config = System.Configuration.ConfigurationManager.ConnectionStrings;
for (int i = 0; i < config.Count; i++) {
if (config[i].Name.Equals(_connectionStringsName, StringComparison.OrdinalIgnoreCase)) return config[i].ToString(); } return String.Empty; }
protected void btn_GetConn_Click(object sender, EventArgs e) {
this.lblMessage.Text = GetConnectionString(this.tb_connName.Text.Trim()); } |
The front end page looks something like this:
| <div align="center">
<asp:Button ID="Button1" runat="server" Text="Zip" Width="71px" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="UnZip" Width="76px" OnClick="Button2_Click" /> </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 front end SeekConnectionStringCsharp.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> |
The flow for the code behind page is as follows
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
// 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 } public string GetConnectionString(string _connectionStringsName) {
System.Configuration.ConnectionStringSettingsCollection config = System.Configuration.ConfigurationManager.ConnectionStrings; for (int i = 0; i < config.Count; i++) {
if (config[i].Name.Equals(_connectionStringsName, StringComparison.OrdinalIgnoreCase)) return config[i].ToString(); } return String.Empty; } protected void btn_GetConn_Click(object sender, EventArgs e) {
this.lblMessage.Text = GetConnectionString(this.tb_connName.Text.Trim()); } } |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!