0

I am using VS2008. I have a project, SystemSoftware project, connect with a database and we are using L2E. I have a RuntimeInfo class which contains some shared information there. It looks like this:

public class RuntimeInfo
    {
        public const int PWD_ExpireDays = 30;

        private static RuntimeInfo thisObj = new RuntimeInfo();

        public static string AndeDBConnStr = ConfigurationManager.ConnectionStrings["AndeDBEntities"].ConnectionString;

        private RuntimeInfo() {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>Return this singleton object</returns>
        public static RuntimeInfo getRuntimeInfo()
        {
            return thisObj;
        }
}

Now I added a helper project, AndeDataViewer, to the solution which creates a simple UI to display data from the database for testing/verification purpose.

I don't want to create another set of Entity Data Model in the helper project. I just added all related files as a link in the new helper project.

In the AndeDataViewer project, I get the connection string from above RuntimeInfo class which is a class from my SystemSoftware project as a linked file. The code in AndeDataViewer is like this:

public class DbAccess : IDisposable
    {
        private String connStr = String.Empty;
        public DbAccess()
        {          
            connStr = RuntimeInfo.AndeDBConnStr;
        }
}

My SystemSoftware works fine that means the RuntimeInfo class has no problem there. But when I run my AndeDataViewer, the statement inside above constructor,

connStr = RuntimeInfo.AndeDBConnStr;

, throws an exception. The exception is copied here:

System.TypeInitializationException was unhandled
  Message="The type initializer for 'MyCompany.SystemSoftware.SystemInfo.RuntimeInfo' threw an exception."
  Source="AndeDataViewer"
  TypeName="MyCompany.SystemSoftware.SystemInfo.RuntimeInfo"
  StackTrace:
       at AndeDataViewer.DbAccess..ctor() in C:\workspace\SystemSoftware\Other\AndeDataViewer\AndeDataViewer\DbAccess.cs:line 17
       at AndeDataViewer.Window1.rbRawData_Checked(Object sender, RoutedEventArgs e) in C:\workspace\SystemSoftware\Other\AndeDataViewer\AndeDataViewer\Window1.xaml.cs:line 69
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
      ....
InnerException: System.NullReferenceException
       Message="Object reference not set to an instance of an object."
       Source="AndeDataViewer"
       StackTrace:
            at MyCompany.SystemSoftware.SystemInfo.RuntimeInfo..cctor() in C:\workspace\SystemSoftware\SystemSoftware\src\systeminfo\RuntimeInfo.cs:line 24
       InnerException: 

I cannot understand this because it looks fine to me but why there is an exception? we cannot access static variable when a class is a linked class? A linked class should be the same as the local class I think. "Linked" here means when I add file I use "Add As Link".

how can I avoid this exception?

EDIT:

I added the SystemSoftware's App.config to AndeDataViewer as a link. Then fixed that problem but I don't like it. How about if I have App.config in there already? I don't want to manually copy connectionstring there either because "manually" means no good.

for those have multiple projects share one database, this link may be helperful when you have trouble: MetadataException: Unable to load the specified metadata resource

Community
  • 1
  • 1
5YrsLaterDBA
  • 33,370
  • 43
  • 136
  • 210
  • What data type is `ConfigurationManager.ConnectionStrings`? I'm assuming `Dictionary` but then why would you need to fetch the ConnectionString from it? – Powerlord Apr 30 '10 at 17:51
  • As for dependencies, you could make `AndeDataViewer` have `SystemSoftware` as a dependency, then access its objects through the `SystemSoftware` namespace, although you'd also need to make `RuntimeInfo` public to do it. – Powerlord Apr 30 '10 at 17:54

3 Answers3

3

You'll see this if your application .config file does not define a connection string name "AndeDBEntities".

The line

    public static string AndeDBConnStr = 
       ConfigurationManager.ConnectionStrings["AndeDBEntities"].ConnectionString;

Is accessing the ConnectionString property of an object returned by ConnectionStrings["AndeDBEntities"] which may not exist.

Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
  • Is that mean in my AndeDataViewer I need to have a reference to the App.config file of the SystemSoftware project? – 5YrsLaterDBA Apr 30 '10 at 17:20
  • References in the project don't really have any relevance at runtime. When you have an App.config in your project, Visual Studio copies it to the build folder and renames it AssemblyName.ext.config. So it doesn't have to be in the project, but it does have to be present wherever you're _running_ the application. – Paul Alexander Apr 30 '10 at 17:25
0

you have exception in your static constructor. create it explicitly and move there all static initializers. then debug :)

Andrey
  • 59,039
  • 12
  • 119
  • 163
0

The AndeDataViewer project doesn't have a the configuration entry. Assuming this is a Winforms app, you need an app.config defined in that project.