Welcome to Dream.In.Code
Become a VB Expert!

Join 137,262 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,507 people online right now. Registration is fast and FREE... Join Now!




Using openfile dialogs and stream reader and witer

 
Reply to this topicStart new topic

Using openfile dialogs and stream reader and witer, IM BEGGING FOR HELP WITH THIS PROGRAM

CanadaRules
26 Nov, 2006 - 01:26 PM
Post #1

New D.I.C Head
*

Joined: 15 Nov, 2006
Posts: 10


My Contributions
THIS IS FOR VISUAL BASIC 2005

ok so you type a 3 digit UPC number in the text box and you hit a button to see it that 3 digit upc is contained in the .dat file called products.dat

if it is contained then it lists the item in a seperate text box.

the products.dat file is set up as follows:

UPC
Descritpion
Price
UPC
Descritpion
Price
UPC
Descritpion
Price
UPC
Descritpion
Price

and so on.....

i need it to read one line at a time and enter the new item everytime you put in another upc. Its doing it once then will not do it again. Please help i am so stuck right now. Here is my code:

CODE

Option Strict On
Imports System.IO
Public Class Form2
    Dim infile As StreamReader
    Dim outfile As StreamWriter
    Dim UPC As String
    Dim Desc As String
    Dim Price As String



    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        infile = New StreamReader(OpenFileDialog1.FileName)
        With OpenFileDialog1
            .InitialDirectory = "C:\Documents and Settings\Michael J Fisher\Desktop\24070\CashReg\CashReg\products.dat"
            .FileName = "prodcuts.dat"
            .CheckFileExists = True
            If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then
                MessageBox.Show("You have selected cancel so now the program will close")

            End If
        End With

    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

    End Sub

    Private Sub EnterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnterButton.Click


        Do While infile.Peek >= 0
            UPC = infile.ReadLine
            Desc = infile.ReadLine
            Price = infile.ReadLine



            If UPCTextBox.Text = UPC Then
                Reciept.Text = UPC & Desc & Price
                
            End If
        Loop



    End Sub
End Class


This post has been edited by jayman9: 26 Nov, 2006 - 01:59 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Using Openfile Dialogs And Stream Reader And Witer
26 Nov, 2006 - 02:15 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Because your StreamReader object is a class level variable and you never close the file after the first time you search through it. The pointer for your StreamReader is at the end of the file, you need to close the file and then reopen to reset the pointer.

Basically it works the first time you search then because the pointer is at the end of the file the Peek method is returning -1, so your while loop never executes again.

You need to close the StreamReader object after you are finished with it, then open when you need to access it again.
User is offlineProfile CardPM
+Quote Post

CanadaRules
RE: Using Openfile Dialogs And Stream Reader And Witer
26 Nov, 2006 - 02:19 PM
Post #3

New D.I.C Head
*

Joined: 15 Nov, 2006
Posts: 10


My Contributions
i thought that was the problem and i have been trying that but cannot figure out where this needs to go.

i used:
CODE
infile.close()


is that correct?
i out that inside of the loop but that did not work.

Any hints as to where this needs to go, if indeed that is the correct code i need?

This post has been edited by CanadaRules: 26 Nov, 2006 - 02:19 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Using Openfile Dialogs And Stream Reader And Witer
26 Nov, 2006 - 03:12 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Yes that is the correct syntax and it needs to be outside of your loop inside your EnterButton_Click method. Should be the last line of code in that method.
User is offlineProfile CardPM
+Quote Post

CanadaRules
RE: Using Openfile Dialogs And Stream Reader And Witer
26 Nov, 2006 - 04:27 PM
Post #5

New D.I.C Head
*

Joined: 15 Nov, 2006
Posts: 10


My Contributions
ok i got one more problem with this.....

it displays the product and price and all that stuff, but when i enter a new UPC it replaces the existsing one instead fo droping down and adding it on a new line. Do i need a
CODE
vbNewLine
or somethign like that? i have the text box already set to multiline as TRUE
Im trying the vbNewLine but if thats what i need i cant figure it out.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Using Openfile Dialogs And Stream Reader And Witer
26 Nov, 2006 - 05:47 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You need to add to the current contents of the textbox instead of replacing whats in it.
CODE

Reciept.Text = Reciept.Text & UPC & Desc & Price & ControlChars.NewLine

User is offlineProfile CardPM
+Quote Post

CanadaRules
RE: Using Openfile Dialogs And Stream Reader And Witer
26 Nov, 2006 - 06:15 PM
Post #7

New D.I.C Head
*

Joined: 15 Nov, 2006
Posts: 10


My Contributions
i give many kudos to you sir

THANK YOU VERY MUCH!!!! biggrin.gif
User is offlineProfile CardPM
+Quote Post

dedman
RE: Using Openfile Dialogs And Stream Reader And Witer
11 Dec, 2006 - 06:49 PM
Post #8

D.I.C Head
Group Icon

Joined: 22 Apr, 2005
Posts: 52


My Contributions
I don't know if this makes any difference but you have 'products.dat' in one place and 'prodcuts.dat' in another.
With OpenFileDialog1
.InitialDirectory = "C:\Documents and Settings\Michael J Fisher\Desktop\24070\CashReg\CashReg\products.dat"
.FileName = "prodcuts.dat"

This post has been edited by dedman: 11 Dec, 2006 - 06:51 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 07:04PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month