Cancel DataAdapter.Fill ()

Scenario: We have a DataGridView that is bound to a DataAdapter (datatable), we load the data into a datatable using (adapter.fill (query, datatable)) in a separate stream (using the delegate and beginInvoke), and as soon as the data loads, we attach this data to datatable to datagridview (in main stream)

Is there a way to check if fill () is executing and cancel it.

Real scenario: The user clicks on the username and the corresponding data is loaded into the datagrid. Once a user is impatient and clicks on another user (here I want to cancel the previous fill and start a new fill)

UPDATE: We save two DataApdaters (and two DataTables), and we attach one datatable to a datagridview and start loading data into another asynchronous asynchronous. When the data is loaded, we simply bind the datagridview to the DataTable we just filled in (and start loading the previous asynchronous input). Thus, the user interface will always receive current data (without waiting for the user interface to update or freeze)

+5
source share
3 answers

You can provide the SqlCommand constructor for the adapter constructor and call the Cancel method on it. There is a raw template:

class Model 
{
    private SqlCommand loadUserCommand;
    private DataTable userData;

    public void LoadUser(string userId) 
    {
        loadUserCommand = GetUserLoadCommandForUserID(userId);
        userData = new DataTable("userData");
        using (var adapter = new SqlDataAdapter(loadUserCommand)) 
        {
            adapter.Fill(userData);
        }
    }

    public void AbortLoadUser()
    {
        if (loadUserCommand!= null)
            loadUserCommand.Cancel();
    }


    private SqlCommand GetUserLoadCommandForUserID(string userId)
    {
        var connection = new SqlConnection("...");
        var command = connection.CreateCommand();
        ...
    }
}
+1
source

DataAdapter.Fill().

, , . async asyn-. . , .

, , , , .

0

I did a quick search and found this: undo the DataAdapter.Fill It seems that there is no way to get around the exception handling, since the author is the state of the code.

0
source

All Articles