Database App.config configurate file Error & Solution

Error message 1

Object reference is not set to an instance of the object.

Error Message:

[Problem Analysis]: Open the App.config file, check the string in the name node and the current brackets is the same, the comparison found, not the same, more than one S. Remember, C# reads the name in brackets used in the node, and the configuration file node configuration name must be consistent!


[problem-solving] remove “s” and run it.

[other Error] 1: some friends don’t pay attention to this situation that you add spaces in the string will also lead to this problem, such as the following:

Spaces must be removed. You cannot add any spaces in the configuration file.

[other Error] 2: if the node position is written incorrectly, the above error will also occur. For example, in the screenshot below, the node should be written to the arrow position.

[other Error] 3: If the App.config configuration file is not added, or the App.config file is not placed in the project launchable project directory, the above error will also occur. For example, the following is wrong:

The following green part is the project’s startable items, App.config file must be put here to work.

[Other Error] 4: modified the name of the configuration file, the configuration file requirements must be the default App.config to work, if you modify the file name, there will also be errors, such as the following is not allowed: the case of.


Error message 2

The type initial value setting item of “ThreeLayer.DAL.SQLHelper” raises an exception.

Error Message:

The problem is that as long as the prompt “SQLHelper’s type initial setting item raises an exception”. Generally, it is a configuration file problem, because we use App.config configuration file to save the data connection string. For beginners, because they can’t understand this error, so they don’t know where to start, here please remember the following troubleshooting methods.

[Solution 1] The configuration file node is written wrong

Originally this place node name is also the above is less than an s, change to the following can be: [Solution 2] configuration file App.config file if you change the name, not placed in the “startable project” root directory.

[Solution 2] configuration file App.config file if the name is modified, not put into the “startable project” root directory, some VS versions will also appear the above error. (This is the same as the solution to error 1)

According to the above method, you can perfectly solve the common problem of getting the connection string through App.config. I hope today’s content can help you.

Read More: