Solved

c# catch exception with access database

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

630 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