Solved

MYSQL Timeout in C# app

Posted on 2011-03-24
4
954 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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