Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I had a DB that normally I access from my SharePoint (2010) custom feature by Entity Framework. The connection string is stored in the web.config.

If I do something like that from outside TimerJob:

using (DB db = new DB()) { }

I get a working instance, if I do the same thing from TimerJob I get exception. To avoid this I stored a connection string into my "Configuration List" so that my code now is:

string cs = //GET STRING FROM LIST
using (DB db = new DB(cs)) { }

but this generates me an exception.

Where is the problem?

I tried with these connection string: metadata=res:///NewDB.csdl|res:///NewDB.ssdl|res://*/NewDB.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.;Initial Catalog=CNH_MW;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework'

OR

metadata=res:///NewDB.csdl|res:///NewDB.ssdl|res://*/NewDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=CNH_MW;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"

without any success..

Exception: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid
Stacktrace:    at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) 

at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName) at CNH.WorkFlowManufacturing.CNH_MW..ctor() at CNH.WorkFlowManufacturing.DBHandler.get_GetCNH_MW() at CNH.WorkFlowManufacturing.JobImportazione.ImportaFile(SPFile file, SqlConnection& conn, Int32 PK_LOG) at CNH.WorkFlowManufacturing.JobImportazione.Execute(Guid targetInstanceId)

Any suggestion? Thank you very much!

share|improve this question
1  
Can you please add the message/stack from the exception you are experiencing? –  SPArchaeologist Mar 4 '13 at 14:24
    
Added right now –  Ziba Leah Mar 4 '13 at 14:25
add comment

1 Answer

up vote 2 down vote accepted

The OWSTIMER doesn't run in a web context and does not have immediate access to your web application web.config files.

You can use something like this tutorial to read web.config values from your web apps: http://praveenbattula.blogspot.com/2009/12/access-webconfig-in-sharepoint-timer.html

You can also create an app.config file right next to the OWSTIMER.exe file (would be owstimer.exe.config).

And lastly, you could store that data in a different fashion (i.e. Farm Properties in the Object Model).

EDIT: Here is an example of creating the OWSTIMER config file: http://doitwithsharepoint.blogspot.com/2011/03/config-file-for-sharepoint-custom-timer.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.