Results 1 to 4 of 4

Thread: Hmmm...TextReader suddenly stopped working...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Hmmm...TextReader suddenly stopped working...

    I had this piece of code working just fine:

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                
                StreamReader sr = new StreamReader("file.txt");
                while (sr.Peek() >=0)
    
                {
    
                    comboBox1.Items.Add(sr.ReadLine());
                    sr.Close();
                }
     
                
            }
    But then suddenly, it starts giving me this error:

    Cannot read from a closed TextReader.

    Why this all of a sudden???

    Complete code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    
    namespace TrialAndError
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                
                StreamReader sr = new StreamReader("file.txt");
                while (sr.Peek() >=0)
    
                {
    
                    comboBox1.Items.Add(sr.ReadLine());
                    sr.Close();
                }
     
                
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                MessageBox.Show("Selected text: " + comboBox1.Text);
            }
    
           
            private void button2_Click(object sender, EventArgs e)
            {
                StreamWriter sw = File.AppendText("file.txt");
                sw.Write(textBox1.Text);
                sw.Close();
                
            }
        }
    }
    Last edited by carstenht; Feb 26th, 2007 at 03:54 PM.

  2. #2
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    Re: Hmmm...TextReader suddenly stopped working...

    You are closing the streamreader in each iteration. You should only close it after you are done peeking at the file:
    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                
                StreamReader sr = new StreamReader("file.txt");
                while (sr.Peek() >=0)
    
                {
    
                    comboBox1.Items.Add(sr.ReadLine());
                }
                sr.Close();
                
            }

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: Hmmm...TextReader suddenly stopped working...

    Yeah i saw that after looking at the code intensively for 10 mins, but thx

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Hmmm...TextReader suddenly stopped working...

    If you're using .NET 2.0 you should use File.ReadAllLines:
    Code:
    comboBox1.Items.AddRange(System.IO.File.ReadAllLines("file.txt"));
    Either way you should always specify your version when posting.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width