Posted on 2006-05-18
Last Modified: 2010-04-23
I have a form with a datagrid and  a search button.

This search could retrieve from 1 to 100 million records.

I would like to able to cancel the search process either by having:

1- cancel button on the same form: which will cancel the database retrieval
2- by opening a new form with a progressbar  and a cancel button:
when you press the cancel button will send a database a cancel retrieval

Thanks for your help
Question by:elianexex
    1 Comment
    LVL 34

    Accepted Solution

    You will need to use either a DataReader (which I would probably go for) or a DataAdapter with its .Fill arguments including StartRecord and MaxRecords - see this

    If you declare (within a scope available to your data reading procedure)

        Dim CancelRead As Boolean = False

    you could, whether on the same form or another form, set it to True if the user pressed the Cancel button.

    You can then interrupt the DataReader's reading process with

       While MyDataReader.Read
          'your code to use the datarow read
          If CancelRead Then Exit While
       End While

    or, with a DataAdapter, something like

       Dim firstRecord As Integer = 0
       Dim batchSize As Integer = 1000
       Dim returned As Integer
          returned = MyDataAdapter.Fill(MyDataSet, firstRecord, batchSize, MyTable)
          If returned < batchsize Then Exit Do
          If CancelRead Then Exit Do
          firstRecord += batchsize

    I've not tested this code, by the way.  Its principles are OK, but I'm not guaranteeing the syntax or my typing.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now