1

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();
5
  • tried using try..catch to get the exact error?? Commented Dec 31, 2008 at 17:04
  • Could it be related to this bug? Commented Dec 31, 2008 at 22:55
  • unlikely, as I downloaded the libraries yesterday, and based on the dates in that bug, the patch should be included in the current release. Commented Dec 31, 2008 at 23:21
  • Can you add the stack Trace to this? I think that would be helpful... Commented Apr 24, 2009 at 17:59
  • The issue was related to my server in the end. The datasource, while properly defined, could not be located, but it was not giving a sensible error message. Commented Apr 24, 2009 at 19:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
No, but the constructor itself can reference a null object within its body, therefore throwing a null reference exception.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.