Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading user information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Content Obsolete

This content is no longer current. We recommend you try the following url for more up to date content: http://channel9.msdn.com/Series/C-Fundamentals-for-Absolute-Beginners

while Iterations and Reading Data from a Text File - 11

12 minutes, 21 seconds


Right click “Save as…”

In this lesson we learn a new type of iteration statement (while) as well as how to utilize the StreamReader class to stream data from a file to the Console window. Additionally, we learn how to add new files to our project, how to set properties of our file using the Properties window, how to add a using statement as a means of resolving a class name referenced in our code to the namespace in which it is defined, and more.


Download the source code for while Iterations and Reading Data...


Follow the discussion

  • Oops, something didn't work.

    Getting subscription
    Subscribe to this conversation
  • CameronCameron

    I love the LOST reference! :D.

  • Smiley  I've added a few "easter eggs" in those examples.  Keeps it fun for me.

  • peterpeter


    Thank you, enjoying the C# lessons.
    Just finished the while loop.
    Looking a head, would the while loop be used to join text two files
    or tsql tables as C# is faster?


    Peter Sidi

  • @peter: Hi, no I can't recommend a loop for either of these scenarios.

    re: two text files ... I would read all the text into memory (I think I demo how to access a file in this series) into either an array or into a list of objects (again, later in this series) then use LINQ to Object (later in this series) to match / join them.  I would have to see the data to make a final determination on the right technique.

    re: two T-SQL tables ... no, I would use a SQL JOIN statement .. which join (INNER, RIGHT, LEFT, OUTER) would depend on the data.  Alternatively, I could use LINQ to SQL or LINQ to Entities, depending on the client I wanted to create for this application.  If that last part didn't make any sense, just keep pushing forward and learning ... you'll get there soon!  Best wishes!!!

  • JoannaJoanna

    Hi Bob,

    Thanks for posting these videos. They are very informative and helpful.

    I'm using Visual Studio 2008 and i don't get that drop down menu suggesting "studio.io" (or anything else). I also don't get red squiggles underneath my text when I write something wrong, though I do get blue squiggles sometimes. Is there a way I can change my options so I get the drop down suggestion box that you get? The red squiggles?

  • @Joanna:  I'm so sorry, but it's been months since I originally recorded that.  Could you tell me the position in the video (like @ 3:55, for example) when you see that?  That would help me pinpoint the time in the video you are referencing.

    I'm pretty sure Visual Studio 2008 has this feature ... so off the top of my head I'm not sure why you would not be seeing that.  Could you possibly post a screen shot?  Maybe I'm just not understanding.  Sorry to be so dense!

  • JoannaJoanna

    Thanks, @BobTabor.

    The issue comes up specifically at about 7:25 in your video. After watching you use the pulldown menu on a number of other videos, I've started to be able to find it more easily (though oddly, sometimes I find it at the beginning of the word and sometimes at the end). I can get the squigle lines after I hit debug, but it seems that you get the squigle lines as you type. Is there a setting I can change to allow me to get the underscore squigles as I type?

    Thank you again.

  • @Joanna:  Sorry for the long delay prior to this response!  I don't know this to be sure, but I would say that this might be an issue due to the processor speed or memory on your computer.  I think Visual Studio is looking through its library of known keywords, data types, operators, etc. trying to pre-parse your code (sometimes called "pre-compilation") which, in fact, makes the ACTUAL compilation of your code into an assembly (.exe or .dll file) a lot shorter as a result.  It's a computationally intensive operation, and needs a lot of power to do it well.  I could be totally wrong here, but I would start by asking what type of computer are you running this on?

  • @Joanna: I had a similiar issue getting the prompts to react. My first issue was that I was trying to get the prompt while in debugging mode. Once I stopped it, I was able to get the prompts.

    The second problem I had seeing it came because I clicked in between text, but then left my cursor on the same line. In that instance, the error message box was coming up before the drop down was, and covered it up so I couldn't see it. If you move your mouse cursor just above the line, I could then see the drop down box in addition to the error message prompt.


  • Aamir SyedAamir Syed

    Very effective lectures!Presents clear C# concepts to those familiar with Object Oriented programming concepts in Java.

  • @Aamir Syed: Thank you!

  • KeithKeith

    I was attempting to read other files than .txt files and in doing so I have found that PDF's and Word docs cause the system to beep on every ReadLine. It also does not pull in the data as it does in a TXT file. Why is this and how would you read in this data from other file formats?

  • @Keith:  I'm not sure about the beep, however .txt files are filled with ASCII text, whereas .docx and .pdf have a proprietary data format that (if I'm not mistaken) has some binary -- or at the very least -- encoded information.  So, you would probably need a (commercial?) third-party component of some kind to assist with that task.  Otherwise you would have to learn the data format and spend months of writing code to support all of its idiosyncrasies.  Best wishes!

  • @Keith:  I'm not sure about the beep, however .txt files are filled with ASCII text, whereas .docx and .pdf have a proprietary data format that (if I'm not mistaken) has some binary -- or at the very least -- encoded information.  So, you would probably need a (commercial?) third-party component of some kind to assist with that task.  Otherwise you would have to learn the data format and spend months of writing code to support all of its idiosyncrasies.  Best wishes!

  • Thennarasanthens #(: Known is a Drop, UnKnown is an Ocean :)#

    @BobTabor: Hi Bob, i have written the following code and works fine.

                string line = "";                
                line = myReader.ReadToEnd();

    Is this is a correct method or it has any problems over the while loop method which you have written?

  • EvanEvan

    A friend and I have been using your C# tutorials for a couple of days now, and we feel we have at least a basic understanding of most of the common principles. What we were wondering, however, was if there was any particular website that we should visit when it comes to giving ourselves challenges.
    Playing around and seeing what one can do is a great way to learn, but if there was a particular goal to work towards, or specific problem that needed fixing, we feel it would help us greatly in the long run.

    We have already looked at;


    but wonder if there are any you could recommend.

    Thanks in advance,


    P.S: Love the videos, they're a great way to learn and I find I appreciate them far more than any University classroom I've been in.

  • anty-socialantisocial Trying to think but nuthin happens !!

    what do the values 4 8 15 16 23 42 represent?? " inquiring minds want to know " hehehe

  • anty-socialantisocial Trying to think but nuthin happens !!

    since you have to do an explicit CLOSE on a file.  when and where does the file get opened? and if I dont close the file.  will C# take care of it or will i leave stuff hanging around when the program is done?

  • LeeLee

    all has been going well except at the end of the video when I am supposed to change the property of values.txt to "copy when newer". In the video, you click and a drop down arrow appears, however the srop down arrow will not appear on mine so i don't know how to get this to work. I have tried a lot of things and I cant figure it out. Using visual studio 2010 on and windows 7

  • Sorry for the late replies, all.  I've been cooking up more videos for Channel9 ... whoops.  I've already said way too much.

    @thens:  Awesome.  You did good.  I was just taking a long-hand approach to demo the while.  Honestly?  In this scenario?  I would much prefer your technique.  Smiley

    @Evan: Recommendations ... no, outside of my own C# for Absolute Beginners series on LearnVisualStudio.NET where I put together a challenge after each grouping of a dozen videos or so.  Maybe that would help?  Lots of people like those.

    @antisocial: It's a magical sequence of numbers made famous in the hit TV show, "Lost".

    @antisocial: The file gets opened here:

    StreamReader myReader = new StreamReader("Values.txt");

    @Lee: That is quite a pickle ... doesn't sound like a normal situation.  I've never seen that.  Are you sure you have the file selected in the Solution Explorer THEN look at its properties in the Properties window?  Have you tried this after rebooting fresh (crazy stuff happens once in a while .. you never know).  Can you show me a screenshot of what you see?


  • LeeLee

    hey bob, I restarted my computer and the drop down box worked. thanks! also, these tutorials are awesome/addicting. I really want to be able to write a program which can take information from NBA box scores online and be able to analyze these results. I know if I keep learning this will be possible!!

  • @Lee: Awesome to hear that worked.  I've learned to always choose the most simple solutions first.  Having said that, I decided not to ask you if your computer was plugged into the wall ... but you get the idea.  Wink  Your app idea is DEFINITELY possible ... the hardest part will be finding a live feed for free (with no licensing fees) but I know that those feeds exist out there.  Good luck!

  • JordanJordan

    Great videos Bob!! I was just wondering what on earth the 'new' thing does. You had it in the last video as well.

    Help would be appreciated,

    Again, brilliant videos, keep it up

  • @Jordan: Watch lessons 14 and 15 ... we'll discuss creating instances of classes using the 'new' keyword.  In a nutshell ... The new keyword creates a new instance of a class in the computer's memory, then returns a reference to the address in memory where that new object can be accessed.  The variable name merely holds the address to that new object so that, when you need to access it, you can merely use the variable name.  So, you can do this:

    Dog myDog = new Dog();

    myDog.hasFleas = true;



    myDog is an instance of the Dog class, which has at least one property (hasFleas) and one method (Scratch) and perhaps others.  Again, lessons 14 and 15 should help you understand this better.  Best wishes! 

  • Bob, is there a way to call a specific line from the text file?

  • Sorry for the delay ... vacation.  Big Smile

    When you say "call a specific line" ... do you mean:

    (A) Retrieve one line, a specific line of textual data from a file?


    (B) Retrieve a line, a specific line of textual data from a file that represents C# code, then execute it?

  • BillBill

    A little confused , line was set to null before Do while , so why didn't it just break and quit at the start? Does the While not see that line=""? Or does it always run through once? Confused?

  • @Bill: Yes, the do ... while always executes the code in the block once.  That is the value of putting the while at the end instead of using one of the other iteration statements.  It's just a different tool in your tool box.  Frankly?  I don't use that often PERSONALLY but I do see other's using it for certain algorithms and such.  I would encourage you to get how this works firmly implanted in your mind ... create a couple of quick examples for yourself using the various iteration statements to really get a feel for what each of them do differently.  Good luck!

  • FNAtheEEnur

    Hey Bob! Great Lectures!! You sure are a lifesaver!! Smiley

    However, I seem to get stuck on the step that you are taking at 7:25. Even though I am using visual c# 2010 I am not getting the first two options (using System.IO and the other option that I cannot really read). When I try to add "using System.IO" to the 5th line, the red line under streamReader does not go away. I was wondering if I am missing any type of toolboxes or anything.

  • RexRex

    StreamReader file = new StreamReader("Values.txt");

    while (!file.EndOfStream)

  • Hello Bob,

    excellent video tutorial, thanks.

    Just a question, in this video on reading a text file you discuss the "while not null value" in your loop but you initialize the variable with an empty string. According to this MSDN article, null and empty string are not the same (http://msdn.microsoft.com/en-us/library/edakx9da(v=vs.110).aspx">http://msdn.microsoft.com/en-us/library/edakx9da(v=vs.110).aspx). Will you please explain why your code works as it appears to me you should initialize the variable to null instead of empty string ("").

    thanks again,


  • @jjensen: I don't have the example in front of me, however I think I remember it ... let me know if I'm off here.  I think this works because, while I *initialize* the string, that only matters until the next line is read from the file.  If the next line doesn't exist, at THAT point the string will be set to null and it will break out of that while.  Hope that helps.

  • A Big Thanks To you Bob Tabor !

  • SyedSyed

    Hi Bob,

    In this video why are using again if condition in the while loop again to check (line!=null). Can you let us for and while loop which one need to use in real time, I mean, how can we judge which will be better.


  • @Syed: This is a great opportunity to experiment.  Try removing that statement and see what happens.  Smiley  Make sure to set a break point and inspect the values as you hit that line / or where you retrieve the line from the text file.

    Also, important ... there are probably a dozen or more ways to do this.  The way I demonstrated is NOT the ONLY way to do it ... I was trying to not add too many new ideas in this video, so I kept it simple.  But as some others have noted above, you could do it in one or two lines (admittedly, long lines).  Hope that helps!

  • SyedSyed

    Hi Bob,
    Thanks I have checked by adding the break point... :)

  • Sanket LunyalSanket Lunyal

    Hi Bob,
    I have started learning C# like ten days back and trust me this is the most fundamental approach to teach somebody. I love your videos.

    I have a question regarding the following Stream Reader line:

    StreamReader myReader = new StreamReader("values.txt");

    1. why do we need to write values.txt in quotes, I understood its a syntax, but we use " " only in case of strings right.
    2. what exactly "new" keyword does here

  • KrishnanKrishnan

    Hi Bob,
    Your video is quite good. I just wanted to know if I want to read a text file which is kept in any other drive other than the project folder. How will I do it?

  • RonRon

    Hi Bob,

    I'm making a rather tricky program for my defense against the dark arts class to get rid of a Boggart. Can you help me?

    Kind Regards


  • MahdiNathanDrake All men dream, but not equally...

    Why you didn't use .ToString() on line 20 ?

    I Really love your Teaching , Tnx so Much




  • Hermoine GrangerHermoine Granger


    It's pronounced Bogart.
    I can help you if you have a free tomorrow morning. Meet me in the common room at 10.15 Wizarding time. Snape is not going to be happy you left it so late.



  • MahdiNathanDrake All men dream, but not equally...

    What i have to do if i want to Write Something on Values.txt from User ?

  • Bob,

    I am somewhat of a novice in programming and I notice that all your platforms are in a console setting.  Do you have videos utilizing Windows Forms?  I have been following along with the Console concepts which basically is the same as Windows forms with a slight adjustment. 

    I do enjoy your training immensely;howevere; is there a reason for this environment vs Windows? 

  • VarunVarun

    Hi Bob,

    I am a beginner and find your videos extremely informative and helpful.
    I have a doubt regarding the way while loop functions, how is the while function able to move onto the next line in the txt file with out us telling it to. I mean in the for loop we have to increment the value everytime the loop finishes and so it moves on to the next.Sorry i posted the same question in next video by mistake.

Remove this comment

Remove this thread


Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.