Reputation: 5738
I am new to desktop application development and currently building a desktop application using layered architecture (user interface, DAL, BLL).
In web development I used to store the connection string in web.config and my class library was accessing it from there. Please guide me how & where connection string should be stored for DAL in desktop application. I tried to add an app.config
file in my class library and access the connection string like this:
ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString)
But it throuws an error: "Object reference not set to an instance of an object."
Kindly guide me on this. Thanks for your support and sharing.
Upvotes: 3
Views: 5018
Reputation: 81
You can use app.config
or other way you can use simple text file mentioning with connection string in it.
//// Read connection string from the text file
StreamReader sr = new StreamReader(System.Windows.Forms.Application.StartupPath @"C:\ConnectionString.txt");
connStr = sr.ReadLine();
Upvotes: 0
Reputation: 17556
It will be stored in app.config
file and you can access the connection string as you do in web application:
if(ConfigurationManager.ConnectionStrings["connectionstringName"] != null)
{
string connectionString = ConfigurationManager.ConnectionStrings["connectionstringName"].ConnectionString;
}
Upvotes: 2
Reputation: 6184
Any configuration settings for an application whether app settings or connection strings should be placed in the application's app.config file. In case of your desktop application you can add an "Application Configuration" file or "app.config" and place your connection string in there. Any dependency for that application .. e.g. a class library like a DAL will pull the value it needs for its connection string from the application's *.config file.
Upvotes: 0
Reputation: 754240
Yes, in a desktop application, all configuration should be in the app.config
for that application. The class libraries used by this desktop application will get their config from that app.config
by default.
If this line throws an exception:
ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString
then it's most likely because no <connectionStrings>
entry of name "connectionstring" exists. Check for NULL:
if(ConfigurationManager.ConnectionStrings["connectionstring"] != null)
{
string connStr = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
}
Upvotes: 3
Reputation: 1038710
It should be stored in the app.config of the Windows Application and not the class library. Basically when you run your executable there should be a file called Foo.exe.config
(where Foo.exe is the resulting executable of your Windows Application) in the same folder which contains the settings.
So in Visual Studio simply add an app.config file to the WinForms application project and store the settings there. In this case they will be successfully read by your custom library. There's no need to add an app.config
file to your class library project as it will never be used.
Upvotes: 4