I want to connect to sql DB, and I have in my Sharepoint project an App.config (with properties : Build "None", Copy to Output directory"Copy always"), I added a reference to System.Configuration.dll (Version 2), and my app.config is like that :

    <add name="ExchangeRate" connectionString="Data Source=CH-SQL;Initial Catalog=COMMON;User ID=_cb;Password=***" />

I call the connection string in my code :

using System;
using System.Configuration;
 exchangeRate = System.Configuration.ConfigurationManager.ConnectionStrings["ExchangeRate"].ConnectionString;

in my project.exe.config , I can see connectionString

I have the following error :

Object reference not set to an instance of an object

Maybe my app.config is not working correctly

Help please



This is how you define your connection in web.config

      <add name="MyConnectionString" 
         connectionString ="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PUBS.MDF; 
                            Integrated Security=True;User Instance=True" />

And to use the connection string, use the below code.

private SqlConnection con = null;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
  • I tried your solution but it still same error , PS : when I use a string instead of connectionString it connects me
    – BKChedlia
    Aug 17 '16 at 9:45
  • Are you able to connect with DB now ?
    – Muskan
    Aug 17 '16 at 9:53
  • When I put string (and not read from app.config) I am able : string connstr = "Data Source=CH-SQLD;Initial Catalog=LOGS;User ID=***;Password=***"; DataProviderUtility.ExecuteStoredProcedureToSqlDatabase(sqlParam, StoredProcedures.CREATELOG, connstr)
    – BKChedlia
    Aug 17 '16 at 9:55
  • 1
    Have you added this connection string in STS and Central admin web.config file as well, if not pls try that
    – Muskan
    Aug 17 '16 at 10:00
  • could you please give me further details, sorry for the dumb questions : what's STS? I cannot find web.config (it's a solution developed by another person, I am just making few changes)
    – BKChedlia
    Aug 17 '16 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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