making thread sleep does not work properly & sometime it give the error on EndExecuteReader that is it's already ended. So try it with blank loop & declare a bool Flag variable on class level.
protected void Button5_Click(object sender, EventArgs e)
{
SqlConnection dbCon = new SqlConnection();
SqlCommand dbCmd = new SqlCommand();
IAsyncResult AsyncResult;
dbCon.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString + ";Asynchronous Processing = true;";
dbCmd.CommandText = "Select Top 5 Customers.CompanyName,Customers.ContactName,Orders .OrderId,Orders.OrderDate, " +
"Orders.RequiredDate,Orders.ShippedDate From Orders,Customers where Orders.CustomerId=Customers.CustomerId" +
" Order By Customers.CompanyName,Customers.ContactName";
dbCmd.CommandType = CommandType.Text;
dbCmd.Connection = dbCon;
Label2.Text = "<b>Connection String </b>for Async Processing : " + dbCon.ConnectionString;
dbCon.Open();
//AsyncCallback callback = new AsyncCallback(Callback_Method);
//ASyncResult = dbCmd.BeginExecuteReader(callback, dbCmd);
AsyncResult = dbCmd.BeginExecuteReader(new AsyncCallback(Callback_Method),dbCmd);
int ctr = 0;
while (!flag)
{
++ctr;
}
Label1.Text = "<b>Loop Count </b>while till the AsyncResult is not completed : " + ctr.ToString();
}
private void Callback_Method(IAsyncResult AsyncResult)
{
flag = true;
SqlCommand dbCommand = (SqlCommand)AsyncResult.AsyncState;
SqlDataReader dbReader = dbCommand.EndExecuteReader(AsyncResult);
GridView1.DataSource = dbReader;
GridView1.DataBind();
dbCommand.Connection.Close();
//dbCon.Close();
}
|