Link to home
Start Free TrialLog in
Avatar of dinesh_bali
dinesh_bali

asked on

Executing the query

Hi,

I am making Windows Application in C# with SQL 2005

On my click button I am executing a query, Queru has multiple records.

It is like

SqlDataReader objRdr

while (objRdr.Read())
{
}

Now, I have another stop button, now as soon as someone clicks on the stop button from windows application form,

my while loop do not work anymore and everything should stop, I can call my terminate process.

Can anyone help me how to do this

Many thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
also add this line inside the loop if you want to form to be responsive and the user able to click the stop button
unless you are running the loop in another thread other than the main application thread.

while (objRdr.Read() && !stop)
{
    //Do your work
    System.Windows.Forms.Application.DoEvents();
}
If your process of executing the query is very cpu intensive then i suggest you invoke this on a separate thread..this way you have 2 ways of stopping the process

a) Kill your sub thread  by calling abort() or join()
b) as anarki_jimbel said update a string variable value and you can check it each time as mohzedan said