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();
}
}
}
}