Thursday, January 15, 2009

Reading web.config from Timer Job

Scenario:
I wrote a timer job and basically in this timer job I wanted to update the DB depending upon sharepoint information. That means i need to have a connection string to make the connection ( mostly in some config file ). This timer job was attached to a Web Application , so I decided to read the connection string from the Web.config of the Web Application

Solution:
WebConfigurationManager class exposes a method OpenWebConfiguration to make it easier to read.

Code:

Configuration config = WebConfigurationManager.OpenWebConfiguration("/", this.WebApplication.Name);
string _sqlConnectionString = config.ConnectionStrings.ConnectionStrings["DBConnectionString"].ToString();

Article:
Points To Share

1 comments:

Anonymous,  April 27, 2009 at 3:28 AM  

Thanks..this is really useful