Solved

Executing the query

Posted on 2006-11-27
3
250 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:dinesh_bali
3 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 18022709
You want to stop reading you results from the query? But how long does it take to read data? Is it so big?

One possible solution to add another (global) boolean value that you set with your stop button, e.g. bool stop.
Initially it is false, when stop button clicked it becomes true.
I your loop you check:


while (objRdr.Read() && !stop)
{
}
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 18022881
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();
}
0
 
LVL 5

Expert Comment

by:rameedev
ID: 18025558
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
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What's the most efficient way to load up a couple of large table in C#? 5 45
Turn on intranet settings 1 57
Error in script 11 57
Release Dynamically Allocated Memory in C# 3 26
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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