I am running an ASP 4.5 application. One one of the pages the user must answer several questions and the push the button to finish the test. My application uses a text file to analyze the users answers. If the user does everything quickly the application works fine, but when it takes longer then 20 min for him to finish the test I get an exception
Cannot read from a closed TextReader
I do not understand what's wrong, because I open StreamReader only when the button is pressed. This is a part of my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GlobalVariables.surname = Request.QueryString["surname"];
GlobalVariables.name = Request.QueryString["name"];
GlobalVariables.gender = Request.QueryString["gender"];
GlobalVariables.age = int.Parse(Request.QueryString["age"]);
}
Label1.Width = 700;
Button1.Click += new EventHandler(this.Button1_Click);
}
void Button1_Click(Object sender, EventArgs e)
{
var f0= new FileStream(Server.MapPath("./key.txt"), FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(f0);
//.....
sr.Close();
sr.Dispose();
}
Could somebody help me please?
f the page is not a post back, you would want to set up the page as it should be viewed the first time. I would also suggest moving the button click even within the if(!Page.IsPostBack) as well as anything that needs to be setup before a post-back. Move your Stream reader to the else... like so if(!Page.IsPostBack) else { stream reader stuff } and remove the button click even in general since the button causes postback.