0

Can't seem to catch exceptions thrown by the MySQL connector. I intentionally fub the connection string settings and when I hit c.Open() it stops there with the exception. Why wouldn't it get caught? Using Throw New Exception() in that block works as expected.

string cString = string.Format( @"server={0};User Id={1};password={2};Persist Security Info=True;database={3}",
                 tbMySQLServer.Text, tbMySQLUser.Text, tbMySQLPw.Text, tbMySQLDB.Text );
MySql.Data.MySqlClient.MySqlConnection c = new MySql.Data.MySqlClient.MySqlConnection( cString );;
try
{
    c.Open();
    tbMySQLTestResult.Text = "Success!";

    if ( cString != Properties.Settings.Default.commvetgenboxConnectionString )
    {
        DialogResult dr = MessageBox.Show( "Would you like to save this new connection string?",
                    "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question );

        if ( dr == DialogResult.Yes )
        {
            Properties.Settings.Default.commvetgenboxConnectionString = cString;
            Properties.Settings.Default.Save();
        }
    }
}
catch ( Exception ex )
{
    logger.ErrorException( "Error accessing database", ex );
    MessageBox.Show( "Error opening database./r/nSee log for detailed information./r/n" + ex.Message,
                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
    tbMySQLTestResult.Text = "Error";
}
finally
{
    if ( c.State == System.Data.ConnectionState.Open )
    {
        c.Close();
    }
}

Edit: Here is the Exception when I hit c.Open():

MySql.Data.MySqlClient.MySqlException occurred
Message=Access denied for user 'ro'@'localhost' (using password: YES)
Source=MySql.Data
ErrorCode=-2147467259
Number=1045
StackTrace:
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
InnerException: 
ArchiFloyd
  • 1,274
  • 13
  • 20
Cooter
  • 537
  • 4
  • 21
  • Its probably timing out. How long have you waited? Could be server settings too. – Botonomous Apr 17 '13 at 20:52
  • Are you running this code inside IDE? Have you applied any settings that holds the execution when a specific exception is thrown? – shahkalpesh Apr 17 '13 at 20:54
  • Check the settings for exception Handling. Debug -> Exceptions -> Common Language Runtime. You should check the 'User Un-Handled' – Steve Apr 17 '13 at 20:55
  • Do you have a full stack trace? How do you know that the exception is not been thrown in the string.format call or when you instantiate the connection? What about if you place that code too inside the try? – Oscar Apr 17 '13 at 20:55
  • http://stackoverflow.com/questions/8066599/mysql-cpp-connector-throwing-unknownexception-while-connecting try changing `string` to `SQLString`? – Brock Hensley Apr 17 '13 at 21:00
  • @shahkalpesh Yes and not that I'm aware of. – Cooter Apr 17 '13 at 21:22
  • @Oscar I wrapped the entire function with a try and it still stopped at the c.Open(). – Cooter Apr 17 '13 at 21:26
  • My first guess would be as @shahkalpesh says. So are you having error handling in your solution like Elmah? http://code.google.com/p/elmah/ – Yeronimo Apr 17 '13 at 21:27
  • @Cooter, again, out of curiosity, what is the settings for the Exception Handling under the Menu DEBUG, EXCEPTIONS, COMMON LANGUAGE RUNTIME? – Steve Apr 17 '13 at 21:47
  • Verify that you are providing the right credentials and that you are allowed to connect from development host to the DDBB using the MySqlCommand Prompt. The message is clear: your connection attempt has been denied. – Oscar Apr 18 '13 at 09:27

2 Answers2

0

Definitely a timeout issue. Either wait for the connection to timeout based on the default settings, or explicitly set a timeout of like 5 seconds, and you should be able to catch the exception.

Jeff
  • 972
  • 5
  • 11
0

Try modifying your connection string. For MySql the basic format should be along the lines:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Phil Carson
  • 884
  • 8
  • 18