[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

MYSQL Timeout in C# app

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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…

649 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