Cannot read from a closed TextReader expeption in asp.net application

469 views Asked by At

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?

3

There are 3 answers

0
Andrew Grinder On

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.

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

private void DoPostBackStuff()
{
    var f0= new FileStream(Server.MapPath("./key.txt"), FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(f0); 
    //..... 
    sr.Close();
    sr.Dispose();
}
5
Joel Coehoorn On

when it takes longer then 20 mis for him to finish the test I get an exception

That sounds a lot like their session expired. To fix this, I recommend adding some javascript to establish a heartbeat to the web server. The heartbeat will keep the session alive; it doesn't need to do anything other than simply make a request every minute or so, so the server knows you're still there.

0
paparazzo On

In addition to the answer from Joel I would recommend to separate processing the file from reading the file.

List<string> lines = new List<string>();
using (var f0 = new FileStream(Server.MapPath("./key.txt"), FileMode.Open, FileAccess.Read))
{
    string line;
    using (StreamReader reader = new StreamReader(f0))
    {
        while ((line = reader.ReadLine()) != null)
        {    
            lines.add(line);
        }
    }
}

// it would need to be a very big text file to be a memory issue
// do your processing here