System.NullReferenceException

  • Thread starter Thread starter micahl98 via DotNetMonster.com
  • Start date Start date
M

micahl98 via DotNetMonster.com

Hopefully someone here can help me out, I have been beating my head against
this for 2 days.

For some reason I am unable to make a connection to an access database. I
have not had any similar problems connecting to mssql. Here is my code
striped out of its classes:

----------
var mdbConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\foobar\\
be_be.mdb;";

var objConnection : OleDbConnection;
objConnection = new OleDbConnection(connectionStr);

objConnection.Open();
----------

Theres not much to this as you can see... but I am receiving the following
error:

"System.NullReferenceException occured in system.data.dll, Object reference
not set to instance of Object..."

I assume this is because objConnection.Open() isnt working, and when I later
try to call ExecuteReader() from a Command Object theres no valid connection
in place and then it errors-out

Searching high and low for answers I thought it may be a permisson issue, so
I set the actual mdb and the folder its in to have full access via Everyone
(im using windows xp btw), but this didnt change anything.

This should be easy, but I am completely stuck... all help greatly
appreciated. -micah
 
Hi,

This is certainly a weird one. Perhaps something is wrong with your OleDb
provieder?
Can you reproduce it on another computer?
 
It is obvious that you did not pass the defined ConnectionString
(mdbConnStr) to objConnection's Constructor. Instead, you passed an
undefined variable (connectionStr), hence, NullReferenceException.
 
Nope, try passing null instead of a connection string and you'll get this:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.data.dll

Additional information: The ConnectionString property has not been
initialized.
 
There is a difference between passing in "null" and passing in an object that
references "null" :) Unless the sample code he posted is missing something,
he never declared "connectionStr" in his code. .NET has no idea what it is
pointing to. Hence the exception that it is a NullReference.

Again, he may have left that part out of his code snippet..... but based on
the error message and the code presented, I would guess this is the problem.

John Scragg

Miha Markic said:
Nope, try passing null instead of a connection string and you'll get this:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.data.dll

Additional information: The ConnectionString property has not been
initialized.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Norman Yuan said:
It is obvious that you did not pass the defined ConnectionString
(mdbConnStr) to objConnection's Constructor. Instead, you passed an
undefined variable (connectionStr), hence, NullReferenceException.
 
Back
Top