Solved

c# catch exception with access database

Posted on 2014-04-24
4
324 Views
Last Modified: 2014-04-24
Hi

I am working on a winforms c# 2010 application connecting to an MS ACCESS database.

occasionally, users are saying that they are having the same error 'could not update; currently locked'

so what I would like to do is catch this when it arises, wait a few seconds, then loop to try again until the database is updated.

how can I achieve this?

here is a snippet of the code to insert data:

 foreach (string f in fields)
            {
                myFields += f + ", ";
                myValues += "?,";

            }


 OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("INSERT INTO " + table + " (" + myFields + ") Values (" + myValues + ") ", con);

            foreach (string v in values)
            {
                cmd.Parameters.AddWithValue("?", v);
            }

            cmd.CommandType = CommandType.Text;

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {

                throw;
            }

Open in new window

0
Comment
Question by:cycledude
[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
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40019944
You could try something like:
int attempts = 5;
while (attempts > 0)
{
    try
    {
        cmd.ExecuteNonQuery();
        attempts = -1;
    }
    catch (Exception)
    {
       if (attempts > 0)
       {
            --attempts;
            System.Thread.Thread.Sleep(1000);       // wait 1 second before retry
       }
       else
       {
            throw;
       }
    }
}

Open in new window

0
 

Author Comment

by:cycledude
ID: 40020066
thanks i will try this..

how can I throw the error to test it?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40020072
Just comment out the line:
cmd.ExecuteNonQuery();

Open in new window

And replace it with:
thrown new Exception();

Open in new window

0
 

Author Closing Comment

by:cycledude
ID: 40020370
thanks

seems to be working ;o)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

756 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