If I add the code for user to download the file (e.g.
if(File.Exists(FILE_NAME)){......}), the ASP.NET will give the
following timeout error:
Timeout expired. The timeout period elapsed prior to completion of the
operation or the server is not responding.
It seems that the download file code is executing before letting the
previous code to finish. Even the message print code
(message.Text="...";) is not executed.
What could I do to eliminate this problem. These are the codes for my
ASP.NET application.
private void run_Click(object sender, System.EventArgs e)
{
message.Text="";
bool alert=false;
string connectionString =
ConfigurationSettings.AppSettings["connString"];
SqlConnection connection = new SqlConnection(connectionString);
if(option.SelectedValue.ToString()=="C")
{
SqlCommand command = new
SqlCommand("inletter3_uterm",connection);
command.Connection=connection;
command.CommandType=CommandType.StoredProcedure;
command.CommandText = "inletter3_uterm";
.........
connection.Open();
command.ExecuteNonQuery();
connection.Close();
string commandString ="select
rtrim(ref)+'/CEM/Rfd/'+rtrim(user_dept_no)+'/'+substring(convert(char,sel_date,112),3,4)
as ref_val,"+
"rtrim(convert(char,DATENAME(month, iss_date)))+'
'+rtrim(convert(char,day(iss_date)))+',
'+rtrim(convert(char,year(iss_date))) as iss_date,"+
"rtrim(D.title) as title from iv_letter3 L, iv_dept D where
L.user_dept_no = D.dept_no";
SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString,
connectionString);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
DataTable dt = ds.Tables[0];
string FILE_NAME=@"c:\inventry\doc\inlet3ut.txt";
StreamWriter sw = new StreamWriter(FILE_NAME, false,
System.Text.Encoding.ASCII);
foreach (DataRow dataRow in dt.Rows)
{
sw.WriteLine("#"+dataRow["ref_val"].ToString().Trim());
sw.WriteLine(dataRow["iss_date"].ToString().Trim());
sw.WriteLine(dataRow["title"].ToString().Trim());
}
sw.WriteLine("#");
sw.Close();
if(updatedate.Checked==true)
{
SqlCommand command2 = new
SqlCommand("inutmti_upd",connection);
command2.Connection=connection;
command2.CommandType=CommandType.StoredProcedure;
command2.CommandText = "inutmti_upd";
.................
connection.Open();
command2.ExecuteNonQuery();
connection.Close();
}
message.Text="User Termination/Obsolete Letter successful
printed to a file\n";
//if such codes are commented, there is not timeout expired
error
if(File.Exists(FILE_NAME))
{
FileInfo myFileInfo = new FileInfo(FILE_NAME);
String filename = Path.GetFileName(FILE_NAME);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition","attachment;
filename=\"" + filename + "\"");
Response.AddHeader("Content-Length",
myFileInfo.Length.ToString());
Response.Flush();
Response.WriteFile(FILE_NAME);
Response.End();
Response.Close();
}
//end of codes giving timeout expired error
}
}