Send feedback on this topic.
Teradata.Client.Provider
BeginExecuteReader(AsyncCallback,Object) Method
Example 



Teradata.Client.Provider Namespace > TdCommand Class > BeginExecuteReader Method : BeginExecuteReader(AsyncCallback,Object) Method
Delegate that will be invoked after the processing of the request has completed. If one is not required a null is specified.
User defined object. This object is available to the callback procedure by accessing the IAsyncResult.AsyncState property.
Initiates the asynchronous request and generates one ore more result sets from Teradata.
Syntax
'Declaration
 
Public Overloads Function BeginExecuteReader( _
   ByVal callback As AsyncCallback, _
   ByVal state As Object _
) As IAsyncResult
'Usage
 
Dim instance As TdCommand
Dim callback As AsyncCallback
Dim state As Object
Dim value As IAsyncResult
 
value = instance.BeginExecuteReader(callback, state)
public IAsyncResult BeginExecuteReader( 
   AsyncCallback callback,
   object state
)
public:
IAsyncResult^ BeginExecuteReader( 
   AsyncCallback^ callback,
   Object^ state
) 

Parameters

callback
Delegate that will be invoked after the processing of the request has completed. If one is not required a null is specified.
state
User defined object. This object is available to the callback procedure by accessing the IAsyncResult.AsyncState property.

Return Value

Returns a IAsyncResult object.
Exceptions
ExceptionDescription
Indicates the TdParameter.Offset is outside of 0 through array size � 1.
Indicates one or more parameters cannot be converted to Teradata native types.
The behavior is not one of the System.Data.CommandBehavior enumeration members.
Can be thrown because of one of the following errors: 1) TdConnection is not in Open state or is null. 2) The local transaction associated with TdCommand is not the same local transaction associated with the TdConnection --assign local transaction to TdCommand.Transaction property.
The TdCommand is closed / disposed.
Can be thrown because the Data Provider detected an error.
Remarks

The call to BeginExecuteReader returns after the request has been successfully sent to Teradata. Any exceptions that are generated by the provider while initializing the request will be thrown during the call to BeginExecuteReader. Exceptions that are generated by the provider or Teradata during the processing of the request will be thrown when TdCommand.EndExecuteReader is called.

The DDL statement CREATE PROCEDURE cannot be specified in an asynchronous execution. If it has been specified a TdException will be thrown when TdCommand.EndExecuteReader is called.

A connection can only have one active request. Therefore, if an asynchronous request is being processed by the provider an exception will be thrown when an attempt is made to start another asynchronous request.

The TdCommand.CommandTimeout property is not applicable to asynchronous execution and will be ignored when a command is asynchronously executed.

Refer to TdCommand.ExecuteReader for more information.

Example

This example shows how BeginExecuteReader can be used in a Winforms Application.

When the Callback has been invoked by the provider, it will not be executing on the Application�s thread. Therefore, the callback cannot have any interaction with the form or any of it�s components. One way to solve this problem is to call the form�s Invoke method to invoke a callback on the application�s thread.

public partial class MainForm : Form
{
    //
    // Indicates the a query is being processed
    //
    Boolean _processing = false;
            
    TdConnection _cn = null;
    String _connectionString = @"Data Source=Teradata1; " +
               "Initial Catalog=ProdDB; " +
               "Integrated Security=True;Pooling=False";
            
    private delegate void WarehouseGridDelegate(TdDataReader warehouseReader);
            
    public MainForm()
    {
        InitializeComponent();
    }
            
    private void WarehouseGrid(TdDataReader warehouseReader)
    {
        try
        {
            //setting up the data table that will be 
            //passed onto the DataGrid to display the 
            //warehouse locations.
            DataTable warehouseTable = new DataTable();
            warehouseTable.Load(warehouseReader);
            
            //populating the data grid.
            this.dataGridWarehouse.DataSource = warehouseTable;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "ERROR");
        }
        finally
        {
            warehouseReader.Close();
            _cn.Close();
        }
    }
            
    private void WarehouseCallback(IAsyncResult result)
    {
        TdCommand command = (TdCommand)result.AsyncState;
            
        TdDataReader warehouseReader = command.EndExecuteReader();
            
        _processing = false;
            
        //Need to setup a delegate that will be invoked by 
        //the form's Invoke method.  The reason for this is that
        //this callback will be running on a different thread than the
        //form's thread. Interaction of the form and its contents 
        //can only be done from the main thread, not from a 
        //worker thread.
        WarehouseGridDelegate wg = 
                  new WarehouseGridDelegate(WarehouseGrid);
            
        this.Invoke(wg, warehouseReader);
    }
             
    private void buttonSubmit_Click(object sender, EventArgs e)
    {
        //checking whether the form is already processing another query
        if (true = _processing)
        {
            MessageBox.Show("Currently retrieving warehouse information");
        }
        else
        {
            String warehouseSQL = "select ProdCount, WarehouseName, " +
                                    "WarehouseCity, WarehouseState " +
                                    "from ProductWarehouse " +
                                    "where ProdNo= " + textBoxProdNo.Text";
            
            try
            {
                _processing = true;
            
                _cn = new TdConnection(_connectionString);
            
                TdCommand command = _cn.CreateCommand();
                command.CommandText = warehouseSQL;
            
                //setting up the delegate for the asynchronous call
                AsyncCallback callback = 
                         new AsyncCallback(WarehouseCallback);
            
                //asynchronous call.  The WarehouseCallback callback will be
                //invoked by the provider after processing of the query has
                //been completed.
                command.BeginExecuteReader(callback, command);
            }
            catch (TdException t)
            {
                MessageBox.Show(t.Errors[0].Message);
            
                _cn.Close();
            }
        }
    }
}
Requirements

Target Platforms: Windows 8.1, Windows 10, Windows Server 2012 R2, Windows Server 2016, Windows Server 2019

See Also

Reference

TdCommand Class
TdCommand Members
Overload List