sunsilk10
July 7th, 2009, 10:08 AM
Hi, I have to provide users a means to cancel a long task (sql quer) in an asp.net application.
For this, I am trying to create a thread to run the query, then when user clicks on cancel button, it will cancel the sql using sqlcommand.cancel (code below). When user clicks on OK or Cancel, the Execute Command is always run.
I cannot get this to work. I do not have the sqlcommand that was fired in the thread. Any suggestions how to implement this?
Thanks
public override void ExecuteTask()
{
if (Input == null) return;
object[] inputs = Input as object[];
string arg = (string)inputs[0];
if (arg.ToUpper() == "CANCEL REPORT")
{
CancelQuery();
return;
}
oracleConn = new OracleConnection();
oracleConn.ConnectionString = "Data Source=" + sDatabase + ";User Id=" + sUser + ";Password=" + sPassword;
string[] org = new string[2];
try
{
oracleConn.Open();
}
catch (System.Exception ex)
{
throw new Exception("Error connecting to Oracle Database. " + ex.Message);
}
string sQuery1 = "select DISTINCT a.NAME, a.POST_CODE from GIS_OWNER.POI_ALL a, DYNAMIC_COVERAGE_ALL b where sde.st_intersects(a.shape, b.shape) = 1 and ";
string sQuery2 = "select DISTINCT a.NAME, a.POST_CODE from GIS_OWNER.POI_ALL a, DYNAMIC_COVERAGE_ALL b where ";
oCmd1 = new OracleCommand(sQuery1, oracleConn);
oCmd2 = new OracleCommand(sQuery2, oracleConn);
t1 = new Thread(new ThreadStart(this.DoQuery));
t1.Start();
t1.Join();
//Create CSV and stream to client
if (dataTable2 != null)
{
MemoryStream memoryStream = GetTextAsMemoryStream(dataTable2);
Map1.Page.Session["Report_FileToDownload"] = memoryStream;
memoryStream.Close();
string mapfilename = "blackhole_report.txt";
string jsFunction = String.Format("BlackholeReport_DownloadFile('{0}');", HttpUtility.UrlEncode(mapfilename));
CallbackResult cr = CallbackResult.CreateJavaScript(jsFunction);
CallbackResults.Add(cr);
}
HideLoadingIndicator();
}
private void CancelQuery()
{
System.Threading.Thread.Sleep(500);
oCmd1.Cancel();
oCmd2.Cancel();
oracleConn.Close();
}
private void DoQuery()
{
DataTable dataTable1 = null;
try
{
dataTable1 = new DataTable();
dataTable2 = new DataTable();
using (OracleDataReader myReader1 = oCmd1.ExecuteReader())
{
try
{
dataTable1.Load(myReader1);
}
catch (System.Exception ex)
{
throw new Exception(ex.Message);
}
}
using (OracleDataReader myReader2 = oCmd2.ExecuteReader())
{
try
{
dataTable2.Load(myReader2);
}
catch (System.Exception ex)
{
throw new Exception(ex.Message);
}
}
}
catch (Exception ex)
{
;
throw new Exception("Error when excuting query ");
}
for (int i = 0; i < dataTable1.Rows.Count; i++)
{
for (int j = 0; j < dataTable2.Rows.Count; j++)
{
if (dataTable1.Rows[i][0].ToString().ToUpper().Trim() == dataTable2.Rows[j][0].ToString().ToUpper().Trim())
{
dataTable2.Rows[j].Delete();
dataTable2.AcceptChanges();
break;
}
}
}
}
For this, I am trying to create a thread to run the query, then when user clicks on cancel button, it will cancel the sql using sqlcommand.cancel (code below). When user clicks on OK or Cancel, the Execute Command is always run.
I cannot get this to work. I do not have the sqlcommand that was fired in the thread. Any suggestions how to implement this?
Thanks
public override void ExecuteTask()
{
if (Input == null) return;
object[] inputs = Input as object[];
string arg = (string)inputs[0];
if (arg.ToUpper() == "CANCEL REPORT")
{
CancelQuery();
return;
}
oracleConn = new OracleConnection();
oracleConn.ConnectionString = "Data Source=" + sDatabase + ";User Id=" + sUser + ";Password=" + sPassword;
string[] org = new string[2];
try
{
oracleConn.Open();
}
catch (System.Exception ex)
{
throw new Exception("Error connecting to Oracle Database. " + ex.Message);
}
string sQuery1 = "select DISTINCT a.NAME, a.POST_CODE from GIS_OWNER.POI_ALL a, DYNAMIC_COVERAGE_ALL b where sde.st_intersects(a.shape, b.shape) = 1 and ";
string sQuery2 = "select DISTINCT a.NAME, a.POST_CODE from GIS_OWNER.POI_ALL a, DYNAMIC_COVERAGE_ALL b where ";
oCmd1 = new OracleCommand(sQuery1, oracleConn);
oCmd2 = new OracleCommand(sQuery2, oracleConn);
t1 = new Thread(new ThreadStart(this.DoQuery));
t1.Start();
t1.Join();
//Create CSV and stream to client
if (dataTable2 != null)
{
MemoryStream memoryStream = GetTextAsMemoryStream(dataTable2);
Map1.Page.Session["Report_FileToDownload"] = memoryStream;
memoryStream.Close();
string mapfilename = "blackhole_report.txt";
string jsFunction = String.Format("BlackholeReport_DownloadFile('{0}');", HttpUtility.UrlEncode(mapfilename));
CallbackResult cr = CallbackResult.CreateJavaScript(jsFunction);
CallbackResults.Add(cr);
}
HideLoadingIndicator();
}
private void CancelQuery()
{
System.Threading.Thread.Sleep(500);
oCmd1.Cancel();
oCmd2.Cancel();
oracleConn.Close();
}
private void DoQuery()
{
DataTable dataTable1 = null;
try
{
dataTable1 = new DataTable();
dataTable2 = new DataTable();
using (OracleDataReader myReader1 = oCmd1.ExecuteReader())
{
try
{
dataTable1.Load(myReader1);
}
catch (System.Exception ex)
{
throw new Exception(ex.Message);
}
}
using (OracleDataReader myReader2 = oCmd2.ExecuteReader())
{
try
{
dataTable2.Load(myReader2);
}
catch (System.Exception ex)
{
throw new Exception(ex.Message);
}
}
}
catch (Exception ex)
{
;
throw new Exception("Error when excuting query ");
}
for (int i = 0; i < dataTable1.Rows.Count; i++)
{
for (int j = 0; j < dataTable2.Rows.Count; j++)
{
if (dataTable1.Rows[i][0].ToString().ToUpper().Trim() == dataTable2.Rows[j][0].ToString().ToUpper().Trim())
{
dataTable2.Rows[j].Delete();
dataTable2.AcceptChanges();
break;
}
}
}
}