Solved

MYSQL Timeout in C# app

Posted on 2011-03-24
4
943 Views
Last Modified: 2013-12-17
Hi,

My C# application require reading a value from a remote mysql database.
All seems ok when internet connection is available.
My code works for the above.

However, when there is intermittent internet connection, an error message tends to pop up as attached. Sometime a different error message appear but basically unhandled exception error. I guess one is for connection and one is when mid way executing the query command the connection ended.

I will like to code it such that the message never appears.

I want to be able to know when a connection is available and when it is not,
When available I will like it to continue with what it does, and if not available to ignore it and execute another set of C# commands.

I also need to be able to handle unsuccessful query execution. Will like for it to do another set of C# commands rather then throw out a error message.

Appreciate if someone is able to advise me how to go about these exception handling.
Thanks.


string MyConString = "SERVER="+ servername + ";DATABASE=" + dbname + ";UID=" + uid + ";PASSWORD="+ pwd +";";
using (MySqlConnection connection = new MySqlConnection(MyConString))
{
    using (MySqlCommand command = connection.CreateCommand())
    {
        MySqlDataReader Reader;

        command.CommandText = "select * from pmask where URL='" + axWebBrowser.LocationURL + "'";
        connection.Open();
        Reader = command.ExecuteReader();
        if (Reader.Read())
        {
            string pb1visible = Reader.GetValue(2).ToString();
            string pb1top = Reader.GetValue(3).ToString();
            string pb1left = Reader.GetValue(4).ToString();
            string pb1width = Reader.GetValue(5).ToString();
            string pb1height = Reader.GetValue(6).ToString();
        }
    }
    //connection.Close();
}

Open in new window

error.gif
0
Comment
Question by:LimMH
4 Comments
 
LVL 1

Expert Comment

by:Muralidharand
ID: 35212892
Can you increase the CommandTimeOut = 0 and connection Time also. try to use Connection pool also.
Can you please post stack trace details ? From there only we can come to know, the error isoccured while establishing the connection / while executing the query.....
0
 
LVL 19

Expert Comment

by:Rikin Shah
ID: 35212895
change
   if (Reader.Read())
to
   while (Reader.Read())

 
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35212947
the problem is that you cannot know in advance if the connection is still ok, or possibly during the execution drops.

you have to Catch the exception, and "handle" it. you might implement a automatic "reconnect + rerun query" procedure, but nevertheless, you need, at some point, to inform the user that something abnormal is happening, and that his transaction might not end up in the database.

you cannot just "hide" and "hope".
0
 

Author Closing Comment

by:LimMH
ID: 35212955
Thanks.

I did:

try {
  // the execution of the query command is inside here


}
catch (Exception ex)
{
  // show messagebox here but flag it that message box appears only once
 
}
finally
{
  // close the oonnection here

}
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question