• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

VB AND DATABASE CANCEL

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
or
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
0
elianexex
Asked:
elianexex
1 Solution
 
SanclerCommented:
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

   http://msdn2.microsoft.com/en-US/library/kxs7kbfe.aspx

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
   Do
      returned = MyDataAdapter.Fill(MyDataSet, firstRecord, batchSize, MyTable)
      If returned < batchsize Then Exit Do
      If CancelRead Then Exit Do
      firstRecord += batchsize
   Loop

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

Roger  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now