Question constuctor Object reference not set to an instance of an object

jassie

Well-known member
Joined
Nov 13, 2012
Messages
61
Programming Experience
1-3
In a C# 2008 application I am getting the error message "Object reference not set to an instance of an object."
The line of code that is causing this to occur is the following:
public eDataContext() :
        base(ConfigurationManager.ConnectionStrings["DEVConnectionString"].ConnectionString, mappingSource)

I have a reference set to system.configuration and I have a using System.Configuration.

Below you will find my app.config file and code that I am referring to:
app.config file code:
C#:
<connectionStrings>
   <add name="esample.Properties.Settings.RPCSS_DEVConnectionString"
      connectionString="Data Source=DEV2;Initial Catalog=DEV;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
The actual code:
namespace eclient
{
 using System.Data.Linq;
 using System.Data.Linq.Mapping;
 using System.Data;
 using System.Collections.Generic;
 using System.Reflection;
 using System.Linq;
 using System.Linq.Expressions;
 using System.ComponentModel;
 using System;
    using System.Configuration;
 
 
 
 public partial class eDataContext : System.Data.Linq.DataContext
 {
        [System.Data.Linq.Mapping.DatabaseAttribute(Name = "DEV")]
  private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
  
   
  
  public eDataContext() :
        base(ConfigurationManager.ConnectionStrings["DEVConnectionString"].ConnectionString, mappingSource)
  {
   OnCreated();
  }
  
  public eDataContext(string connection) : 
    base(connection, mappingSource)
  {
   OnCreated();
  }
  
  public eDataContext(System.Data.IDbConnection connection) : 
    base(connection, mappingSource)
  {
   OnCreated();
  }
  
  public eDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
    base(connection, mappingSource)
  {
   OnCreated();
  }
  
  public eDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
    base(connection, mappingSource)
  {
   OnCreated();
  }

Thus can you tell me how to fix this code?
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
Um, I'm wondering whether you read that very carefully. Here's where you define the connection string:
C#:
<add name="[B][U]esample.Properties.Settings.RPCSS_DEVConnectionString[/U][/B]"
and here's where you use it:
C#:
base(ConfigurationManager.ConnectionStrings["[B][U]DEVConnectionString[/U][/B]"].ConnectionString, mappingSource)
You don't see the discrepancy there?

From the name of your connection string, it's obviously been added vis the application settings. As such, that's how you should be accessing it:
C#:
base(Properties.Settings.Default.RPCSS_DEVConnectionString, mappingSource)
 
Top Bottom