Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Tuesday, December 23, 2008

Accessing ConnectionString from CodeBehind

Accessing a ConnectionString from CodeBehind requires your connection string to be defined in the web.config file.

Then use this source code:

ConnectionString = System.Configuration.ConfigurationManager.AppSettings["MySetting"].ToString();

I customized my BasePage with a utility function as follows:

public string getConnectionString(string vConnectionString)
{
System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
System.Configuration.ConnectionStringSettings connString = null;
string connectionString = string.Empty;
if (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count)
{
connString =
rootWebConfig.ConnectionStrings.ConnectionStrings[vConnectionString];
if (connString != null)
connectionString = connString.ConnectionString;
}
return connectionString;
}

References: http://forums.asp.net/p/1353594/2770309.aspx
http://msdn.microsoft.com/en-us/library/ms178411(VS.80).aspx

2 comments:

Anonymous said...

It's difficult to find educated people for this subject, but you sound like you know what you're talking about!

Thanks

Feel free to visit my blog post - windows registry cleaner
My page: windows xp registry cleaner

Anonymous said...

This is my first time pay a visit at here and i am in fact happy to read all
at single place.

my blog post - Best Registry Cleaner For Windows 7
my web page - best registry cleaners