Solved

wait for completion ExecuteNonQuery command

Posted on 2004-08-03
8
1,889 Views
Last Modified: 2006-11-17
How can I force my program to wait for completion ExecuteNonQuery command on Access database?

Thanks

Morfo
0
Comment
Question by:morfo
8 Comments
 
LVL 9

Expert Comment

by:s_sansanwal
ID: 11711255
Could you update more?
I think program by default runs a statement at one time and hence would wait when calling excutenonquery before moving to next line
0
 

Author Comment

by:morfo
ID: 11712757
I have small C# application. I use OleDbConnection to access to mdb database file.

I have listbox on my form where is shown for example list of companies. To insert new company I call (ShowDialog()) new form, where user fill all necessary informations. After click on accept button I call insert command:

myCommand.CommandText = "INSERT INTO companies (xxxx) VALUES (yyyy)";
try
{
      myCommand.ExecuteNonQuery();
}
catch (System.Data.OleDb.OleDbException ex)
{
      MessageBox.Show(ex.Message);
}
this.close();

On end of insert procedure I close dialog form and on main form I call refresh procedure, where I clear my listbox, I access to database and I read new listbox items. And new company is not shown between comapanies items. I need refresh form again.

When after
      myCommand.ExecuteNonQuery();
line I insert line
      MessageBox.Show("Wait a moment!");
and before click OK I wait a few seconds, new company is represented between listbox items. But this is not very cool solution. I want to force my application programatically to wait for executing insert command. How do it?

0
 
LVL 7

Expert Comment

by:Ceiled
ID: 11712889
That's very odd...when I do something similar, it seems to be running synchronously, and I haven't been able to see the old values, even though I look at it immediately after updating it. You might try running the command in a transaction -- call BeginTransaction() on the connection, set the Transaction property of the command that you're executing, then call Commit() on the transaction. If that doesn't help, then I'm not sure where to go from there. Perhaps you somehow toggled a setting that puts the data system into asynchronous mode?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:morfo
ID: 11741994
It doesn't works too.
I first time meet with this problem. Usually I work with SQL Server and there I haven't similar problems .
Possilble is everything, but how can I set it? I have no ideas.
0
 
LVL 7

Accepted Solution

by:
psdavis earned 50 total points
ID: 11754778
I've used this approach a lot with Ado.Net and Access database.  The ExecuteNonQuery is definitely running Synchronously.  That's not your error.

I'm betting you have a totally different error than what you are expecting.

Put another catch in.  I know of a bug where if you are running this in response to an event or in a different thread, the Exception is ingored.

try
{
     myCommand.ExecuteNonQuery();
}
catch (System.Data.OleDb.OleDbException ex)
{
     MessageBox.Show(ex.Message);
}
catch( Exception ex )
{
   MessageBox.Show( ex.Message );
}
0
 

Author Comment

by:morfo
ID: 11760031
I try it and no exception appears. I have some other parts of code in try statement after ExecuteNonQuery command. And this code allways runs correctly. All my application runs in one thread. I think that the only one explanation for my problem is that myCommand running asynchronously.

Morfo
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

862 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

23 Experts available now in Live!

Get 1:1 Help Now