Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
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#.

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;

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

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

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

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>

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

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

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

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks