dc

dc

  • NA
  • 663
  • 0

C# constructor "Object reference not set to an instance of an object."

Nov 15 2012 1:32 PM
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:

<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?

Answers (20)