Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Advanced Technologies - Get Connectionstring using ASP.NET 2.0 and C#
Get Connectionstring using ASP.NET 2.0 and C#


ASP.NET Advanced Technologies Tutorial

This tutorial will show you how to get connectionstring from configuration file using ASP.NET 2.0 and C#.

Download the Full Working Version of this Project written with Visual Studio.NET C#.NET 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

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;

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" />
&nbsp; &nbsp; &nbsp;<asp:Button ID="Button2" runat="server" Text="UnZip" Width="76px" OnClick="Button2_Click" />
</div>

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>&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>

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.AspNetTutorials.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());
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C#.NET 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks