Tuesday, March 9, 2010

Get Connection string from web.config in ASP.NET 2.0, 3.5

In Asp.Net 2.0 and Asp.Net 3.5 we have a Tag Inside Web.config file that is ConnectionString Tag.

<connectionStrings>
<add name=”MyConnStringconnectionString=”Server=ServerName;Database=DBName;

Uid=UserId;Pwd=Passwd;providerName=”System.Data.SqlClient/>
connectionStrings>


Now to get the Connection string defined in the web.config file you have to do like this in your .cs  file. Below code is based on C#

string Conn= ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;


In Vb.NET
Dim Conn as String = ConfigurationManager.ConnectionStrings(“MyConnString“).ConnectionString

No comments:

Post a Comment