Solved

MYSQL Timeout in C# app

Posted on 2011-03-24
4
913 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 142

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now