Question find data in file?

blindguarden

New member
Joined
Jun 21, 2007
Messages
1
Programming Experience
Beginner
First of all let me say Hello to everone.

I am useing VB.NET 2003

I have finially decided to break down and ask for some help with my program lol.
let me explain what I am trying to do before I ask my questions.
I am creating a program that will write to a file the product code and the price of that product on different lines. Then I want to code an inputbox that will read only the price of the product string that is entered in to it (example. if I enter JKL45 I want the app. to display only the price of that Item that is stored in the sequential access file).
Now my question is how would I go about doing that?
I have the StreamWriter.WriteLine part all ready coded to write the product string and price to separate lines. I can get the app. to read every line in the file line by line but that is not what I need it to do. I just need it to search for the product string and only display the price of the product that is entered into that inputbox.

thanks in advance.
Blindguarden
 
The only way to read a text file is line by line. You simply need to read each pair of lines until the first of the pair is your item ID, then get the price of that item form the second line, e.g.
VB.NET:
Dim sr As New IO.StreamReader("file path here")

Do Until sr.ReadLine() = itemID
    sr.ReadLine()
Loop

Dim price As Decimal = Convert.ToDecimal(sr.ReadLine())

sr.Close()
That assumes that the file is in the appropriate format and the item ID is present. Some validation and error handling would be a good idea.
 
Back
Top