none
C# Object reference not set to an instance of an object

    Question

  • 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?
    Thursday, November 15, 2012 6:29 PM

Answers

  • Hello Wendy,

    Change this line :

    base(ConfigurationManager.ConnectionStrings["DEVConnectionString"].ConnectionString, mappingSource)

    To :

    base(ConfigurationManager.ConnectionStrings["esample.Properties.Settings.RPCSS_DEVConnectionString"].ConnectionString, mappingSource)

    Alternatively, you can change the name of your ConnectionString to "DEVConnectionString" in your app.config file :

    <connectionStrings>   <add name="DEVConnectionString"      connectionString="Data Source=DEV2;Initial Catalog=DEV;Integrated Security=True"      providerName="System.Data.SqlClient" />  </connectionStrings>



    Thursday, November 15, 2012 6:38 PM

All replies

  • Hi!

    I think the connection string name is esample.Properties.Settings.RPCSS_DEVConnectionString.

    Try with this:

    public eDataContext() :
            base(ConfigurationManager.ConnectionStrings["esample.Properties.Settings.RPCSS_DEVConnectionString"].ConnectionString, mappingSource).

    Regards!

    Thursday, November 15, 2012 6:35 PM
  • Hello Wendy,

    Change this line :

    base(ConfigurationManager.ConnectionStrings["DEVConnectionString"].ConnectionString, mappingSource)

    To :

    base(ConfigurationManager.ConnectionStrings["esample.Properties.Settings.RPCSS_DEVConnectionString"].ConnectionString, mappingSource)

    Alternatively, you can change the name of your ConnectionString to "DEVConnectionString" in your app.config file :

    <connectionStrings>   <add name="DEVConnectionString"      connectionString="Data Source=DEV2;Initial Catalog=DEV;Integrated Security=True"      providerName="System.Data.SqlClient" />  </connectionStrings>



    Thursday, November 15, 2012 6:38 PM
  • What you suggested worked fine for the code I showed to you! However, I still have the same problem
    in a second project file in the same solution file. Basically here is what I have for the second project file with a second app.config file.
    Can you tell me what you would change here name?


    app.config

     <connectionStrings>
        <add name="RPTtest.Properties.Settings.DEV_ESTConnectionString"
          connectionString="Data Source=DEV;Initial Catalog=DEV-_EST;Integrated Security=True"
          providerName="System.Data.SqlClient" />
      </connectionStrings>

    code
    namespace RPTtest
    {
     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;
     
     
     [System.Data.Linq.Mapping.DatabaseAttribute(Name="DEV_EST")]
     public partial class RPTtestDataContext : System.Data.Linq.DataContext
     {
      
      private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

      
      public RPTtestDataContext() :
                    base(ConfigurationManager.ConnectionStrings["RPTtest.Properties.Settings.DEV_ESTConnectionString"].ConnectionString, mappingSource)
      {
       OnCreated();
      }
      
      public RPTtestDataContext(string connection) :
        base(connection, mappingSource)
      {
       OnCreated();
      }
      
      public RPTtestDataContext(System.Data.IDbConnection connection) :
        base(connection, mappingSource)
      {
       OnCreated();

    yes i did what you suggested for the first problem and that fixed  it.

    However for the second problem I thought I would mention the  following additonal items:
    1. settings.settings file shows the connection  string = DEV_ESTConnectionString. The application settings would not allow me to  change the value to RPTtest.Properties.Settings.DEV_ESTConnectionString.
    Should I edit the file in notepad so that it would allow me to change the  connection string name?
    2. Looking at the connection string properies of the  datacontext object it is showing me the following:
    a. application settings=  true,
    b. application setting name is DEV_ESTConnectionString
    c. The  connection string information is not what I specified in the app.config file. it  was a value that I used previously.

    What would you suggest I try now?



    Thursday, November 15, 2012 8:07 PM
  • http://en.csharp-online.net/CSharp_FAQ:_What_does_Object_reference_not_set_to_an_instance_of_an_object_mean
    Thursday, November 15, 2012 10:01 PM
  • Hi Wendy!

    Ok. So the application is working now?

    I guess the application is using the connection string from the app.config, not the settings file...

    Regards! 

    Thursday, November 15, 2012 10:23 PM