8

I have two projects. One is web & the other is windows forms. Web project connects to database but the windows project throws an exception NullReferenceException reading the connection string.

I am using the same classes to connect both projects. Connection is established using LINQTOSQL.

Here is my Connection string:

<connectionStrings>
    <add name="GPSystemConnectionString"
         connectionString="Data Source=.;Initial Catalog=GPSystem;User ID=***;Password=***"
         providerName="System.Data.SqlClient" />
</connectionStrings>

This is how i am reading it.

string CS = ConfigurationManager.ConnectionStrings["GPSystemConnectionString"].ConnectionString;

(Exception occurs on this line)..... (Object reference not set to an instance of an object.)

Note: i am using same class to connect both projects.... one connects but the other fails.

Please any one help me with this!

Thank you in advance.

5
  • 5
    You need to have the appropriate entry in the app.config (for WinForms) and web.config (for the Web project) - these configurations are read for the executing assembly and are not shared. Commented Jan 30, 2014 at 7:23
  • @user2864740: It is possible to share them. See my answer. Commented Jan 30, 2014 at 8:22
  • @PatrickHofman Your answer really just steals the configuration ;-) Commented Jan 30, 2014 at 9:18
  • @user2864740: Isn't it wonderful? ;) I use this a lot. I have an own ORM mapping that reads the config file from the ExecutingAssembly. This method is really useful. Commented Jan 30, 2014 at 9:20
  • if you want to use a file for both projects you need to include it in your apps and if web project can read config just do it for another like this var configs = new ConfigurationBuilder() .AddXmlFile(Path.Combine(AppContext.BaseDirectory, "same.config")) .Build(); check this Configuration Builder Commented Jul 6, 2022 at 7:51

4 Answers 4

12

You need to have entry in app.config for connection string in your window application.

If you don't have App.Config file then add it.

and put entry like below

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="GPSystemConnectionString"  connectionString="..." />
  </connectionStrings>
</configuration> 

In you .cs file

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

var connectionString=ConfigurationManager.ConnectionStrings["GPSystemConnectionString"].ConnectionString;
Sign up to request clarification or add additional context in comments.

Comments

8

I always struggle with this issue of null reference with the ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString when I have the data access layer in a different project/library than the executable. I often use the following to help debug the location of the configuration file at runtime:

var configfile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var path = configfile.FilePath;

2 Comments

This is a great tip to find out the root cause. I had the same problem when testing the LinqToSql code in the unit test project. In the end, I have to add the connection string in the app.config of the unit test project.
@fradsham - Great tip!
4

If you want to get the configuration section from another assembly use this:

Assembly assembly = ...; /* get the assembly to read config from */

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(assembly.Location);

string CS = configuration.ConnectionStrings["GPSystemConnectionString"].ConnectionString;

2 Comments

Can this be used to access the web.config from the WinForms application (as opposed to the app.config of another application)? I've never used this before.
@user2864740: The OP says that it shares classes, so I guess it also shares a common assembly. In that case this is a decent solution. It is also possible to read the web.config but you need the WebConfigurationManager.
0

In my case this issue was due to ConnectionString in my 64bit Machine.Config

C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\Config

Rather than the standard 32-bit

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config

To correct remove the Prefer 32-bit setting on the Build tab of Project Properties Build tab of Project

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.