O'Reilly Forums: Inner Exception: Object Reference Not Set To An Instance Of An Object - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Inner Exception: Object Reference Not Set To An Instance Of An Object Rate Topic: -----

#1 User is offline   Ulfius 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 21-November 11

Posted 21 November 2011 - 11:51 AM

When taking advantage of the OnContextCreated event in my Entities, I'm receiving this error when I press the Test button on the connection.

"The database connection is valid, but the following exception was thrown when trying to instantiate the custom ObjectContext.

Exception has been thrown by the target of an invocation.

Inner Exception: Object reference not set to an instance of an object."


If I press OK to add the connection anyways, when running a query, I also get "Object reference not set to an instance of an object."

Here's my OnContextCreated -

Partial Class MyEntities
Inherits ObjectContext

Private Sub OnContextCreated()
Connection.ConnectionString = GetEntityConnection()
End Sub

End Class


Is there a way for me to take advantage of the event to set the connection string without needing to comment out that line whenever I want to test queries with LinqPad?

This post has been edited by Ulfius: 21 November 2011 - 11:53 AM

0

#2 User is offline   Ulfius 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 21-November 11

Posted 17 December 2011 - 04:06 PM

Joe,

Is there any better workaround to my issue?

TIA,
Shawn
0

#3 User is offline   JoeAlbahari 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 529
  • Joined: 15-February 08
  • Gender:Male
  • Location:Perth, Australia

Posted 17 December 2011 - 06:20 PM

Have you tested to see what's null?

Is it the Connection property - or is the exception being thrown GetEntityConnection()?

Have you tried this:

if (Connection == null)
Console.WriteLine ("null connection");
else
Connection.ConnectionString = GetEntityConnection();

The stack trace on the exception should give you more clues, too
0

#4 User is offline   Ulfius 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 21-November 11

Posted 19 December 2011 - 01:47 PM

View PostJoeAlbahari, on 17 December 2011 - 06:20 PM, said:

Have you tested to see what's null?

Is it the Connection property - or is the exception being thrown GetEntityConnection()?

Have you tried this:

if (Connection == null)
Console.WriteLine ("null connection");
else
Connection.ConnectionString = GetEntityConnection();

The stack trace on the exception should give you more clues, too


Maybe I wasn't clear enough that it's not my application that has the issue. My application works fine with the code in the OnContextCreated event of the entity, but LinqPad throws the error if I try to reference the assembly without commenting out the "Connection.ConnectionString = GetEntityConnection();" line. GetEntityConnection() is returning a valid entity connection string.

Ideas?

This post has been edited by Ulfius: 19 December 2011 - 01:48 PM

0

#5 User is offline   JoeAlbahari 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 529
  • Joined: 15-February 08
  • Gender:Male
  • Location:Perth, Australia

Posted 20 December 2011 - 08:19 PM

You can think of LINQPad as an IDE like Visual Studio. If adding a line of code causes an error, it makes sense to put debugging code in there to track down what's going on.

The reason you don't also get an error when running from a project created in Visual Studio could be to do, for instance, with a different application configuration file or initialization code that's running (or not running)

This post has been edited by JoeAlbahari: 20 December 2011 - 08:42 PM

0

#6 User is offline   Ulfius 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 21-November 11

Posted 05 January 2012 - 02:48 PM

Joe,

Although I have error trapping and logging code in my application, it was not logging anything. By replacing the line "Connection.ConnectionString = GetEntityConnection()" with "Connection.ConnectionString = My.Settings.EntitiesPprd", it worked correctly, then through process of elimination, I tracked it down to when you try to access a project via LinqPad, HttpContext.Current is not valid. Here is my GetConnectionString procedure.


	Public Function GetEntityConnection() As String
		Try
			If HttpContext.Current.Server.MachineName = My.Settings.PROD Or My.Settings.ForceProdDb = "True" Then
				Return My.Settings.HamptonEntitiesProd
			End If
			Return My.Settings.HamptonEntitiesPprd
		Catch ex As Exception
			NotifyOfError(ex)
		End Try
	End Function


Changing it to this works as expected now.

	Public Function GetEntityConnection() As String
		Try
			If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.Server.MachineName = My.Settings.PROD Or My.Settings.ForceProdDb = "True" Then
				Return My.Settings.EntitiesProd
			End If
			Return My.Settings.EntitiesPprd
		Catch ex As Exception
			NotifyOfError(ex)
		End Try
	End Function


I do know that checking for that condition should always be done, but this was just a small project and of course when you run it in Visual Studio, it launches its own IIS instance and HttpContext.Current would "always" exist when it would be hitting that part of the code.

Thanks.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users