Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I'm using the MySQL Connector for .net applications, specifically, C#. A System.Net.Sockets.SocketException is thrown when I invoke MySqlConnection.Open(). I've been looking over this code for the past few days, decided to see if another pair of eyes could help me.

I'm on Windows 7 x64 using MySQL Connector Net 6.3.0 in Microsoft Visual C# Express Edition. I have already linked to the MySql.Data.dll in C:\Program Files (x86)\MySQL\MySQL Connector Net 6.3.0\Assemblies. I don't receive any warnings nor errors. I have to explicitly catch this error or else my application will crash.

I've done a bit of digging around, one answer posted online said that I may have too many network adapters installed, which is not the case for me. I've disabled any unnecessary drivers for each adapter as well. I will be testing this problem in a virtual machine once I setup the development environment, I will post back with any results.

The only other thing that I can think of is that I had installed multiple other connectors for MySQL, along with a driver I have compiled for the Qt framework. I've since removed all of these, to no avail.

Any help is much appreciated. As I said, this has had me pulling my hair for the past two weeks. I'm almost to the point of just blaming MySQL and falling back a version or two of the connector if possible.

A first chance exception of type 'System.Net.Sockets.SocketException' occurred in MySql.Data.dll
"The system detected an invalid pointer address in attempting to use a pointer argument in a call".

What else I've tried:
- Microsoft Fix it 50203
- Removing gratuitous network adapters, drivers and MySQL connectors
- Windows update, for what it's worth
- Reset Winsock controller (netsh winsock reset)

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace MySQLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            MySQLHandler MySQLh = new MySQLHandler();

            if (MySQLh.connectToServer())
                MySQLh.disconnectFromServer();
        }
    }

    class MySQLHandler
    {
        private MySqlConnection connection;
        private string server = string.Empty, user = string.Empty;
        private string pwd = string.Empty, database = string.Empty;

        string connectionString = string.Empty;

        public MySQLHandler()
        {
            server = "localhost";
            database = "qauth";
            user = "root";
            pwd = "";

            connectionString = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + user + ";PASSWORD=" + pwd + ";";
            connection = new MySqlConnection(connectionString);
        }

        public MySQLHandler(string server, string user, string password, string database)
        {
            this.server = server;
            this.database = database;
            this.user = user;
            this.pwd = password;

            connectionString = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + user + ";PASSWORD=" + pwd + ";";
            connection = new MySqlConnection(connectionString);
        }

        public bool connectToServer()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Attempting to connect to server...");
            Console.WriteLine();

            try
            {
                connection.Open();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Connection successfully established with " + this.server + "!");
                return true;
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                Console.WriteLine("A socket exception was thrown! " + ex.Message);
                Console.ReadKey();
                return false;
            }
            catch (MySqlException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                switch (ex.Number)
                {
                    case 0:
                        Console.WriteLine("Cannot connect to the specified server. Contact network administrator");
                        break;

                    case 1045:
                        Console.WriteLine("Invalid username or password.");
                        break;
                    default:
                        Console.WriteLine("An unknown error has occured, no connection to the server was established.");
                        break;
                }

                return false;
            }
        }

        public bool disconnectFromServer()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Attempting to disconnect from server...");
            Console.WriteLine();

            try
            {
                connection.Close();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Disconnected from server successfully!");
                return true;
            }
            catch (MySqlException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Failed to disconnect from " + this.server + " (" + ex.Message + ")");
                Console.WriteLine();
                return false;
            }
        }
    }
}
Posted
Updated 19-Nov-12 9:13am
v5
Comments
Kuthuparakkal 18-Nov-12 22:53pm    
Are you sure you got blank password for root ?
JayFab 18-Nov-12 22:59pm    
Yep. This same connection string works on many other applications that I develop in the WAMP environment. Here is my MySQL user table (for the account i'm attempting to use)

[USER] root
[HOST] localhost
[PASSWORD] No
[GLOBAL PRIVILEGES] ALL PRIVILEGES
[GRANT] Yes

There you have it, the most secure account in MySQL's history. I'm fairly certain this has something to do with the implementation of the connector's DLL. It's not on my end either, I don't think. I'm looking for an older version of the connector now.

Found older versions (will post back with results): http://dev.mysql.com/downloads/connector/net/6.5.html#downloads
Kuthuparakkal 18-Nov-12 23:12pm    
Try : http://www.codeproject.com/Articles/71346/Connecting-to-MySQL-Database-using-C-and-NET
JayFab 18-Nov-12 23:31pm    
Sorry, I've already tried this. No matter how elegantly I write the code, the exception is thrown.
Herman<T>.Instance 19-Nov-12 4:42am    
what server address: IP or Name and is thwe Port available through firewalls?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900