Reputation: 88
connectionString = "SERVER=localhost;DATABASE=NT;UID=" + dbuser + ";PASSWORD=" + dbpass + ";";
connection = new MySqlConnection(connectionString);
OpenConnection();
//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
Console.WriteLine(" >>>> Cannot contact MySQL Server ");//MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
Console.WriteLine(" >>>> Invalid username/password, please try again");
break;
case 1042:
Console.WriteLine(" >>>> Unable to resolve DNS");
break;
}
return false;
}
}
The code above is successfully connecting to the localhost database and logging in but the database "NT" doesn't exist (which I know) but the ex.Number property is set to 0 I also got the same issue with wrong username/password combo. The exception message/text gives me the string of the error but the number field is always set to 0. Any help would be greatly appreciated.
Many Thanks, Ben
EDIT/UPDATE
The only difference between the 2 screengrabs is that I changed the NT to test (NT doesn't exist (which the message reports correctly but not the number) and test does exist)
I'm wondering if this is maybe intended behaviour? Is it basically refusing the connection because that DB doesn't exist?
Upvotes: 0
Views: 2499
Reputation: 572
Having had the same problem myself, it seems that the Number
field is only filled in the inner exception. I'm not sure why this is the case but the MySqlException
contains an inner exception also of type MySqlException
, and the inner exception has the number.
So to get the number you should probably use something like this:
int number;
if (ex.InnerException != null && ex.InnerException is MySqlException)
{
number = ((MySqlException) ex.InnerException).Number;
}
else
{
number = ex.Number;
}
This includes a null check and type check for the inner exception, and falls back to the number in the outer exception if there is no inner exception or it's the wrong type.
Upvotes: 0
Reputation: 11
I am using MySql Connector .Net 6.9.4 and also had this issue. I found out that there is an InnerException of type MySqlException that has the Error Number. You can try my Helper Method:
public static int GetExceptionNumber(MySqlException my)
{
if (my != null)
{
int number = my.Number;
//if the number is zero, try to get the number of the inner exception
if (number == 0 && (my = my.InnerException as MySqlException) != null)
{
number = my.Number;
}
return number;
}
return -1;
}
Greetings
Frank
Upvotes: 1
Reputation: 1
The same problem generated me, too. I think the bug of Connector/Net. Please correct in the following code and try.
switch (ex.InnerException.Number)
{
It was tested in 6.9.8.
Upvotes: 0
Reputation: 11607
Don't set PASSWORD
but PWD
like this:
connectionString = "SERVER=localhost;DATABASE=NT;UID=" + dbuser + ";PWD=" + dbpass + ";";
Upvotes: 0