RobertasJ
RobertasJ

Reputation: 19

Problems with entity framework

I'm having problems working with EntityFramework. While below peace of code works fine on my PC, when it's transported to a VPS (with everything properly preinstalled), it gives me a Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object, but the message box that is supposed to catch this does not show up. Any suggestions?

Thank you in advance.

        var cc = new CopierContext();
        try
        {
            MessageBox.Show(cc.Database.Connection.ConnectionString.ToString());

            var matchingProviders2 = cc.Providers.Where(prov => prov.Login == "batman");
        }
        catch (Exception e)
        {
            MessageBox.Show(e.InnerException.Message);
        }

Update:

I finally got to the core of the problem. The reason is: I've had .NET 4 on VPS, while application was developed using .NET 4.5. Installing the latter one removed all problems. Thank you for all your help.

Upvotes: 1

Views: 283

Answers (4)

Conrad Frix
Conrad Frix

Reputation: 52675

Well from the docs on the Exception.InnerException Property

The InnerException property returns the same value as was passed into the constructor, or a null reference

Since you're catching any old exception catch (Exception e) its quite possible that the exception that's being thrown isn't the exception you were expecting and doesn't have a InnerException. This means your catch block may be raising an exception.

There are several actions you could take.

  1. Do not catch System.Exception exception in anything but a top level exception handler. Only catch exceptions you know what to do with. Which leads to...
  2. Set up a top level exception handler
  3. Finally when logging or displaying exception messages at least make sure you have an inner exception before you try and use it.

    MessageBox.Show( (e.InnerException != null ? e.InnerException : e).Message );

Upvotes: 1

Pawel
Pawel

Reputation: 31620

My bet is that you did not configure something (or did not configure it correctly) and cc.Database.Connection.ConnectionString is null. Calling .ToString() causes the NullReferenceException. That's why you don't see the message box. As other people said - using MessageBox for this kind of debugging is not a good idea. In the catch you should print not message but e.ToString() it will show the stack trace that should point to the place where the problem is.

Upvotes: 0

Justin Harvey
Justin Harvey

Reputation: 14682

I don't know how you are running it on your VPS, but if it is not launched under the interactive user account, your message boxes will not show up.

Upvotes: 1

Tamara Wijsman
Tamara Wijsman

Reputation: 12348

Don't use a MessageBox as it is a service; log them to disk instead such that you can recall them, or perhaps automatically mail them to you such that you are up to date on problems occuring.

You will also want to add e.InnerException.StackTrace to the log.

Upvotes: 0

Related Questions