views:

75

answers:

3

Hey all,

I'm using Visual Basic 2008 here, and I'm debugging my code that connects to my SQL Database and writes something in it. It was working fine and all until I came to an error like this. NullReferenceException was unhandled. What's going on? Here is the code I'm working with:

Dim conn As MySqlConnection
                    conn = New MySqlConnection
                    conn.ConnectionString = "server=...; user id=...; password=...; database=..."
                    Try
                        conn.Open()
                    Catch myerror As MySqlException
                        MsgBox("Error connecting to database")
                    End Try
                    Dim myAdapter As New MySqlDataAdapter
                    Dim sqlquery = "SELECT * FROM user WHERE username = '" + TextBox2.Text + "'"
                    Dim myCommand As New MySqlCommand()
                    myCommand.Connection = conn
                    myCommand.CommandText = sqlquery
                    myAdapter.SelectCommand = myCommand
                    Dim myData As MySqlDataReader
                    myData = myCommand.ExecuteReader()

It highlights it right around conn.open(), and gives me that error. It was working fine earlier until I moved my sql database to my mac. (windows -> mac) Is there a difference? I backed up my stuff from my windows vista computer and restored it on my mac. I don't think there is a difference, but I'm just putting that out there. Why does this error come up?

Thanks,

Kevin

A: 

I can't really say what the problem is off the top of my head.

But the way to find out would be to put a breakpoint on the conn=New MySqlConnection and look at the variables you have as you step through.

If the problem is happening on the conn.Open line then it would probably be because conn is null (nothing). But if that is the case then conn.ConnectionString should have thrown an exception.

The other step is to look at the stack trace of the exception and see where, exactly, it is being thrown from. It could, for example, be an error somewhere inside the mysql connector.

Sorry I can't really give you a definate answer, but I thought some general comments might help you to the solution or find some more detail.

Glenn Condron
Yea I think its null, though I haven't tried it yet.
Kevin
Ok, I've tried it by putting a breakpoint on it, and it says it has the value of nothing. What should i do?
Kevin
If the connection is nothing then your assignment code must not be working.So I would look at the conn = new MySqlConnection(). I do not know why it is not working, I have used similar code in the past and it worked fine. It could be something to do with the OS I guess, perhaps the version of the connector that you are using. As a first hit perhaps try the other overloads, giving the connection string to the constructor.
Glenn Condron
Ok I fixed the program by upgrading my .NEt CONNECTOR. but the problem now is that I can't get the connection through. In other words, I get the error "Could not connect to Database" like described above. If I do a conn.open without the if statement, I get a comcast.net blah blah blah error. Do you know what the problem is now?
Kevin
I would need the full details of the exception to be able to tell what is, so the full exception message and such. The most common cause of being unable to connect would be either the machine you are on cannot talk to the database server, or the user you are trying to login as does not have the permissions. Remember in MySQL that pemissions are specific to the machine a user comes from. So make sure the user has permissions to access the server from the machine you are using.
Glenn Condron
Never mind I got it to work.
Kevin
A: 

Whivh version of MySql are you using? Have you looked at the details of this bug...

http://bugs.mysql.com/bug.php?id=26393

... which is related to idle time.

If you had the full stack trace associated with the exception then it might shed some light on the problem.

Martin Peck
Should I upgrade to 5.4 Community Server?
Kevin
I wouldn't like to say. It's difficult to tell from your description if this is the same issue or not.
Martin Peck
+1  A: 

I would suggest reviewing your connection string. I used your code and connected to our local MYSQL db no errors.

Michael Eakins