Solved

c# catch exception with access database

Posted on 2014-04-24
4
319 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
  • 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 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

16 Experts available now in Live!

Get 1:1 Help Now