0

i am accessing store proceudre from sql database but it throws at connection string point:

"Object reference not set"

Code:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=HOME-PC;Initial Catalog=LMS;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>


string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();

i am using windows forms in c#.net and i am newbie.

John Nash
  • 155
  • 1
  • 4
  • 12

2 Answers2

1

From one of your comments, it seems perhaps your connection string is defined in the app.config file of a class library. If that's the case, you'll need to copy the connection string entry in the config file to the configuration file of the actual application - in this case, the Windows Forms application that is calling the business layer library.

Rick Liddle
  • 2,684
  • 19
  • 31
  • exactly but why ? if i have put my connection string in app config of Business layer (class library) then why pasting same app file in actually project too ? why 2 app configs if actually connection code in in business layer ? – John Nash Sep 29 '14 at 19:28
  • You don't have to put the connection string in the app.config (I suspect Visual Studio does it for you as a "convenience") and if you distribute that library without its app.config, it won't bat an eye. A class library is not executable on its own and does not have its own AppDomain - it exists within the context of the AppDomain of the calling application. At runtime the .Net framework routes requests for configuration from a class library to the application that is invoking it. I'm looking for a good source of information that explains all of this and I'll post back when I find something. – Rick Liddle Sep 29 '14 at 19:45
  • quite good but i am newbie and couldn't understand technical terms and slangs u used – John Nash Sep 29 '14 at 19:53
  • [This answer](http://stackoverflow.com/a/7263047/35241) from John Saunders explains it in less technical terms. Essentially, different apps may use a class library with different configurations, so it's up to the app to provide the config. – Rick Liddle Sep 29 '14 at 20:05
  • "you'll need to copy the connection string entry in the config file to the configuration file of the actual application" WORKS FOR ME! – Aimal Khan Jun 07 '18 at 05:50
0

I found two possible solutions:
A. Removing the providerName="System.Data.SqlClient" part from the connection string definition on the App.config file
or
B. Adding a "using System.Data.SqlClient;" line on the using section of the client code where you are attempting to get and use connectionString, like this:

using System.Data.SqlClient;

//(...other code...)

string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();

My guess is that the use of the providerName option on the connectionstring forces a call to the specified provider on execution.

Gedeon
  • 742
  • 9
  • 13