?
Solved

MYSQL Timeout in C# app

Posted on 2011-03-24
4
Medium Priority
?
991 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 20

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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

864 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