0

I have an error with connection string in class library project. In Appconfig I have this code:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=DESKTOP-123\SQLEXPRESS;Initial Catalog=MyDb;User ID=sa;Password=mypassword"  providerName="System.Data.SqlClient"/>
  </connectionStrings>

Connection string I call form a static function:

public static string ConnDb {
        get {
            return ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        }
    }

When I use this connection string in function

Sqlconnection conn = new Sqlconnection(Class1.ConnDb)

I have some error like

{"Object reference not set to an instance of an object."}

What can I do?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Optimus
  • 93
  • 2
  • 10
  • Where is the error occurring in the code? Have you stepped through the code to see what object is giving you the null reference? – Barry O'Kane Jun 02 '17 at 10:14
  • Libraries don't have app.config, the running process does so put that connection string in the config of the running project. – Crowcoder Jun 02 '17 at 10:18
  • I have error when i call class to get connectionstring – Optimus Jun 02 '17 at 10:18
  • Possible duplicate of [app.config for a class library](https://stackoverflow.com/questions/5674971/app-config-for-a-class-library) – Federico Dipuma Jun 02 '17 at 10:27
  • App.config need to be in the project which uses this class library. Try that and let us know if you still face the issue – Chetan Jun 02 '17 at 11:41

1 Answers1

1

you can user appSettings for alternative option.

ishan joshi
  • 270
  • 3
  • 13