Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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)
 
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 18-Nov-12 16:44pm
JayFab165
Edited 19-Nov-12 9:13am
v5
Comments
Kuthuparakkal at 18-Nov-12 22:53pm
   
Are you sure you got blank password for root ?
JayFab at 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 at 18-Nov-12 23:12pm
   
Try : http://www.codeproject.com/Articles/71346/Connecting-to-MySQL-Database-using-C-and-NET
JayFab at 18-Nov-12 23:31pm
   
Sorry, I've already tried this. No matter how elegantly I write the code, the exception is thrown.
digimanus at 19-Nov-12 4:42am
   
what server address: IP or Name and is thwe Port available through firewalls?
JayFab at 19-Nov-12 15:11pm
   
I'm using WAMPP web server locally (LAN). Apache is using ports 80 (HTTP) and 443 (SSL). MySQL is using port 3306. I have explicitly configured my firewall to allow this behavior. To be sure, I uninstalled the firewall and restarted. After rebooting, the error was still invoked.
 
Additionally, I have reverted my connector version, which did not help. I still receive the same error.
Bernhard Hiller at 20-Nov-12 3:38am
   
Windows7 64 bit and "Program Files (x86)": is the target of your application x86?
digimanus at 20-Nov-12 5:01am
   
IP4 <-> IP6 problem ? see here

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



Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100