I am building a C# application, and a problem occurred, I have no idea how to fix it though.
I have a couple of forms, and now I want to add a form which is inherited by another. When I want to view the [Design] screen of the new form, it gives me the following error:
Object reference not set to an instance of an object.
Now I found out that the problem was the connection string, which is located in the app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="ConnString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=PVB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
This is my code for the connectionString in C#:
string connectionString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
If I use the string like the one above, I get the Object Reference blabla error. Suprisingly, when I use the following code, the form just works perfectly fine:
string connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=PVB;Integrated Security=True";
Does anyone have a clue how I can fix this problem? Yes, I’ve added the System.Configuration reference to the solution, so that’s not the problem, I guess..
Please review the following URL which might resolve your issue. It seems that the problem here is simulair:
access app.config from inherited form