Print this page
Sunday, 30 December 2012 20:56

using connectionstring of configuration file in pages

Written by 
Rate this item
(0 votes)

normally we define connectionstrings under
<connectionStrings> tag in the web.config
file.membership providers use that
connectionstring.what if we needed to use
the connectionstring in asp.net pages?.

without writing connectionstring codes
again in the page we can use the following
codes in pages to access the connectionstring
of web.config file -

1.add first using System.Configuration;

2.then declare following in appropriate section-

public ConnectionStringSettingsCollection _connections
= ConfigurationManager.ConnectionStrings;

public SqlConnection _connection = new SqlConnection();

_connection.ConnectionString =
_connections["ConnectionStringName"].ConnectionString;

3.now initiate connection by the connectionstring -

SqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection
(_connection.ConnectionString);

Read 2561 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
7