Bug #23920 Error being thrown when connection is opened
Submitted: 3 Nov 2006 8:00 Modified: 9 Dec 2006 8:03
Reporter: Burton Posey Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:5.0.1 OS:Windows (WinXP SP2)
Assigned to: CPU Architecture:Any
Tags: .net, 5.0, beta, connector, Visual Basic

[3 Nov 2006 8:00] Burton Posey
Description:
I had created an application to connect with a mySql server for a school project. I had no problems with the other implementations, but there were limitations so I switch to MySql. The point is I had a sound established connection with the server through Visual Basic and was retrieving some of the information I needed.

Anyways, once I started using the implementation as documented in the included help files with the MySQL Connector 5.0 install, I kept receiving this message.

_______________________________________________________________________________
An unhandled exception of type 'System.NullReferenceException' occurred in MySql.Data.dll

Additional information: Object reference not set to an instance of an object.
_______________________________________________________________________________

I checked and eventually checked 15 times over the connection strings and the like, as well as doing some extensive research on the internet finding people that had a similar problem but their advice did not help.

How to repeat:
With version 5.0 of the connector installed and set up as a reference:
'make sure you are importing this with the other ones
Imports MySql.Data.MySqlClient

Public Sub CreateMySqlConnection(myConnString As String)
    Dim myConnection As New MySqlConnection(myConnString)
    myConnection.Open()
    MessageBox.Show("ServerVersion: " + myConnection.ServerVersion _
        + ControlChars.Cr + "State: " + myConnection.State.ToString())
    myConnection.Close()
End Sub

I would get the error on something as simple as this, knowing that my server settings were correct. It always pointed to the line with myConnection.Open()

Suggested fix:
I could not find a workaround with 5.0, but I do now know for a fact that after uninstalling 5.0, installing 1.0.8 and changing the references accordingly in my vb project, it now works fine time and time again. It also worked as well when I changed the references in the sample file included in the 5.0 release.
[8 Nov 2006 10:02] Tonci Grgin
Hi Burton and thanks for your problem report.
In order to help you I will need the following:
 - Exact MySQL server version, host OS
 - Client OS
 - *Exact* connector/NET version (5.0.x)
 - NET fw version (if on Win)
 - Small but *complete* test case (VS I presume) showing this error

I suspect something's wrong with your code since I am able to work with c/NET without problems.
[8 Nov 2006 18:16] Burton Posey
I believe I had mentioned that the exact same code worked with the older connector (1.0.8), but not the new 5.0.1 beta one. The code that wasn't working... was the as-is table read-on data example from the 5.0.1 connector download, as well as my test case. I believe on the packaged example the problem was being thrown on the reader when I sent my information through with the appropriate button click.

So, in other words, when I changed my references to the older connector dll from the newer one... I had to change no code whatsoever to get both programs to work.

The Host's mySQL version is:
4.1.8-Max

I'm confident that they are running Linux, but not 100% sure. My teacher conducting my independent study doesn't like working with windows and works on databases all day, so I would say Linux.

I'm running WinXP SP2
Using Visual Studio .NET 2005
(I believe I had to up-convert the examples to 2005. Like I said, I can run it fine right now with the older (1.0.8) dll, but something could have changed in the conversion process).

Here is an example that I just retested with the 5.0.1 MySQL.Data Reference in place. This program was tested right before I went to bed last night with the 1.0.8 connector dll reference in place. With 5.0.1, it does not work. With 1.0.8, it works fine. I took all of the personal stuff out and put in the <stuffname>.

    Public Function CreateConnection()

        myConnection = New MySqlConnection
        connStr = ""
        If Not myConnection Is Nothing Then myConnection.Close()

        If connStr = "" Then
            connStr = "Database=<insertdatabasename>;" & _
            "Data Source = <enterdatasource>;" & _
            "User ID=<enteruserid>;" & _
            "Password=<enterpw>;" & _
            "Allow Zero Datetime=true;" & _
            "pooling=False;"
        End If

        myConnection.ConnectionString = connStr

        Try
            myConnection = New MySqlConnection(connStr)

            myConnection.Open()
            Return True
        Catch ex As MySqlException
            MessageBox.Show("Error connecting to the server: " + ex.Message)
        End Try
        Return False
    End Function

The error is thrown at the line MyConnection.Open() and that error is:
An unhandled exception of type 'System.NullReferenceException' occurred in MySql.Data.dll

If you wanted to run MyConnection.Close() before the open statement, it works. I'm not sure if that is worth anything. Thanks for the help.
[9 Nov 2006 8:03] Tonci Grgin
Hi Burton. I asked for *complete* sample. If you want to pursue this further, please attach *entire* VS project.
You have not answered all of my questions:  - NET fw version (if on Win)
Is it possible you're using NET fw 1.x? If so, failure is expected, see ReleaseNotes.

Consider following C# code / environment which works with no problem:

C:\mysql\bin>mysql -uroot test
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11 to server version: 4.1.22-log

NET fw 2.0 on WinXP Pro SP2

//Assembly MySql.Data
    D:\svn-net\tags\5.0.1-beta\mysqlclient\bin\net-2.0\Debug\MySql.Data.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
--
        private void button1_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = "DataSource=localhost;Database=test;UserID=root;Password=;PORT=3306;Allow Zero Datetime=true;pooling=False;";
            conn.Open();
            MySqlCommand command = new MySqlCommand();
            command.CommandText = "SELECT * FROM blacklist";
            command.CommandType = CommandType.Text;
            command.Connection = (MySqlConnection)conn;
            MySqlDataReader dr = command.ExecuteReader();
            MessageBox.Show("Before READ");
            dr.Read();
            MessageBox.Show("After READ");
            MessageBox.Show(dr.GetName(0));
            MessageBox.Show(dr.GetName(1));
            dr.Close();
            MessageBox.Show("Ready");
            command.Dispose();
            conn.Close();
        }
[10 Dec 2006 0:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".