Solved

c# catch exception with access database

Posted on 2014-04-24
4
318 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
Comment Utility
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
Comment Utility
thanks i will try this..

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

Expert Comment

by:Carl Tawn
Comment Utility
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
Comment Utility
thanks

seems to be working ;o)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

744 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

17 Experts available now in Live!

Get 1:1 Help Now