Description:
Hi,
I can connect to Mysql database with C# without problem. But if I change correct password to non-correct one, I should get exception with the number 1045. But it doesn't work. It gives exception with wrong number.
Example:
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnectionStringBuilder connstring = new MySqlConnectionStringBuilder { Server = "localhost", Password = "rooter",UserID="root",Database="newbase" };
MySqlConnection connection = new MySqlConnection(connstring.ToString());
try
{
connection.Open();
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 1049: Response.Write("invalid database");
break;
case 1045: Response.Write("Login failed");
break;
default: Response.Write("The number of exception is "+ex.Number.ToString());
break;
}
}
connection.Close();
}
Above code is without problem. Bu if i will add some character to my password string, I 'm getting output "The number of exception is 0".
Another example: if i'm changing correct database name with the incorrect one (Database="blablabla") . i still get the 0 (zero) exception number.
Only if, when I'm writing incorrect server name(Server = "blahblahblah") then I'm getting correct exception numebr as below:
"The number of exception is: 1042"
How to repeat:
Add Mysql extension to .NET project (I tried with the ASP.NET project). Then paste below script to the Default.aspx.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
public partial class Website_MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnectionStringBuilder connstring = new MySqlConnectionStringBuilder { Server = "localhoslt", Password = "rooter",UserID="root",Database="newbase1" };
MySqlConnection connection = new MySqlConnection(connstring.ToString());
try
{
connection.Open();
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 1049: Response.Write("invalid database");
break;
case 1045: Response.Write("Login failed");
break;
default: Response.Write("The number of exception is: "+ex.Number.ToString());
break;
}
}
connection.Close();
}
}
Suggested fix:
Need to check Mysql Extension for .NET. MysqlException class.