tags:

views:

1231

answers:

3

I'm trying to connect a C# application (using Visual C# 2008 Express Edition) to a remote MySQL server. I have the drivers for this, but when I followed the tutorials (including adding the pooling and connection reset properties), I get an error that: Object reference not set to an instance of an object. I've included the two lines of code that should be making a connection. The error is thrown on the second line.

        MySqlConnection connect = new MySqlConnection("database=d*******;Data Source=mysql.netfirms.com;user id=*******;pwd=*****;pooling=false;connection reset=false");
        connect.Open();
A: 

I'd try setting the connection string outside of the constructor to help narrow down the issue:

MySqlConnection connect = new MySqlConnection();
//Do you get the null exception in this next line?
connect.ConnectionString = "your conn string here";
connect.Open(); //-> If you get the exception here then the problem is with the connection string and not the MySqlConnection constructor.

If you do get the exception in the connect.ConnectionString = ... line, then the problem is with the driver and sounds like you need to reinstall it.

I would also try a simpler connection string, without the pooling and reset keys.

Ricardo Villamil
I get the exception on the connect.Open(); line. I added the pooling and reset keys based on a response to someone with a similar problem, but it didn't make any difference.
Elie
Can C# really return null from a constructor? In Java that's impossible. Either you get a valid object reference or an Exception is thrown.
Daniel Schneller
No, but the constructor itself can reference a null object within its body, therefore throwing a null reference exception.
Ricardo Villamil
A: 

Could it be related to this bug?

Darin Dimitrov
unlikely, as I downloaded the libraries yesterday, and based on the dates in that bug, the patch should be included in the current release.
Elie
+1  A: 

Can you post more code? The exception line is probably a bit off due to compiler optimization or related. Constructors must return an object or throw an exception. It is impossible to say

MyType item = new MyType();
Debug.Fail(item == null); // will never fail.

The null reference is probably on the line just above your instantiation.

Daniel Moore