Results 1 to 16 of 16

Thread: [RESOLVED] I need some help...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Location
    Houston, TX
    Posts
    26

    Resolved [RESOLVED] I need some help...

    I am making a form where I can type in a name, hit find, the program will look through text files and display the information.

    I have attached my form. From my current code I get the results from the second screen shot.

    I have two text files, one is Clients Database.txt and the other is a test customer called John Test.txt. The information in Clients Database will be the names of the text file for each customer, for now it is just John Test.txt.

    In the text file, John Test.txt there is :

    John Test
    [email protected]
    123456789 (item number)
    987654321 (tracking #)
    $250.00 (total sale)

    I would like each line in the John test.txt file to be displayed in the corresponding text boxes in the form.

    Let me know if I need to clarify, I will post my code in the next post.
    Attached Images Attached Images   

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Location
    Houston, TX
    Posts
    26

    Re: I need some help...

    What am I doing wrong?

    PHP Code:
    Public Class frmDatbase

        
    Private Sub btnDisplay_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles btnDisplay.Click
            Dim foundflag 
    As Boolean
            Dim filename 
    As String

            

            Dim name 
    As String " "

            
    Dim email As String " "
            
    Dim itemnumber As String " "
            
    Dim trackinginfo As String " "
            
    Dim totalsale As String " "
            
    Dim sr1 As IO.StreamReader IO.File.OpenText("Client Database.txt")

            
    'the next line is not needed. It is added for clarity
            foundflag = False
            Do While (foundflag = False) And (sr1.Peek <> -1)
                filename = sr1.ReadLine
                Dim sr2 As IO.StreamReader = IO.File.OpenText(filename)
                Dim sr3 As IO.StreamReader = IO.File.OpenText(filename)
                Dim sr4 As IO.StreamReader = IO.File.OpenText(filename)
                Dim sr5 As IO.StreamReader = IO.File.OpenText(filename)
                Dim sr6 As IO.StreamReader = IO.File.OpenText(filename)


                Do While (name <> txtName.Text) And (sr2.Peek <> -1)
                    name = sr2.ReadLine
                    txtName2.Text = name

                    Do While (email <> txtEmail.Text) And (sr3.Peek <> -1)
                        email = sr3.ReadLine
                        txtEmail.Text = email

                        Do While (itemnumber <> txtItem.Text) And (sr4.Peek <> -1)
                            itemnumber = sr4.ReadLine
                            txtItem.Text = itemnumber

                            Do While (trackinginfo <> txtTracking.Text) And (sr5.Peek <> -1)
                                trackinginfo = sr5.ReadLine
                                txtTracking.Text = trackinginfo

                                Do While (totalsale <> txtSale.Text) And (sr6.Peek <> -1)
                                    totalsale = sr6.ReadLine
                                    txtSale.Text = totalsale

                                Loop



                            Loop

                        Loop
                    Loop

                Loop
                sr2.Close()
                If name = txtName.Text Then
                    txtName2.Text = name
                    txtEmail.Text = email
                    txtItem.Text = itemnumber
                    txtTracking.Text = trackinginfo
                    txtSale.Text = totalsale
                    foundflag = True

                End If
            Loop
            sr1.Close()
            If foundflag = False Then
                txtName.Text = "Name not found."
            End If
        End Sub

    End Class 

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

    Re: I need some help...

    The first thing you're doing wrong is using text files for this stuff. Is there a particular reason you're not using a database?
    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

  4. #4
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: I need some help...

    You should use an actual database like Access or SQL. This way, once the customer is found, you can just import the information from the correct columns.

    A text file for this sort of thing gets messy and will most likely end up very bad when you have a lot of customers info.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Location
    Houston, TX
    Posts
    26

    Re: I need some help...

    Ah I did not know that, plus I dont have access to those programs.

    This is just one of my assignments that I just can't seem to figure out so the most customers I'll add is like 5 just to test the program out.

    How would I go about using Access or SQL? Also has anyone found the fault in my code besides using text files?

    Is it possible to find the customer from a textfile and import it in the correct row of text without using a database?

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

    Re: I need some help...

    To use Access you'd have to have the Access application but SQL Server Express and SQL Server CE are both free and integrate into VS or VB Express.

    That said, if this is an assignment then you need to play by the instructors rules. Were you told to expressly use text files?
    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Location
    Houston, TX
    Posts
    26

    Re: I need some help...

    That is true, the example I am following is to simply look up phone numbers through text files, I wanted to take it a little further and decided to do the look up my way but I believe I should still use text files for this assignment.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Location
    Houston, TX
    Posts
    26

    Re: I need some help...

    anyone? I just need a point in the right direction on my coding. Thanks

  9. #9
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: I need some help...

    OK, well if you have to use a text file, the first thing you need to do is make sure the text file data is comma delimited(are you the one designing the structure of the text file, or is someone giving you the text file?). This way you will know where each field ends and where the next one begins and by keeping a consistent format for the text file, you know what each field represents. For example you should represent the above data as:

    John Test,[email protected],123456789,987654321,$250.00

    Then you can code something like this:

    vb Code:
    1. Imports System.IO
    2. Public Class Form1
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Dim Path as String = "C:\...whatever you want"
    6.         Dim AllLines() As String = File.ReadAllLines(Path)
    7.         Dim PiecesOfLine() As String
    8.  
    9.         For Each line In AllLines
    10.             ' Check line to see if it matches the search criteria
    11.             If line.Contains(txtCustomer.Text) Then
    12.                 ' Line in file matches the search criteria so split it up
    13.                 PiecesOfLine = line.Split(",")
    14.                 ' Fill in all the textboxes on the screen with the pieces
    15.                 txtName.Text = PiecesOfLine(0)
    16.                 txtemail.Text = PiecesOfLine(1)
    17.                 ' etc... Get the idea?
    18.                 Exit For
    19.             End If
    20.         Next
    21.     End Sub
    22. End Class

    I am sure there is many ways to do this(another one being using LINQ to search the AllLines collection(array actually)) but this is the one I can think of off the top of my head. You would also probably want to move the line reading to another buttons event so it happens once and not each time the search button is pressed. I will let you play with this. This is just to give you the idea....
    Last edited by The Fire Snake; Apr 15th, 2010 at 11:24 PM.
    Remember to click on the scales to the left and rep me if I helped

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Location
    Houston, TX
    Posts
    26

    Re: I need some help...

    Ah I'm sorry I don't really understand the code...

    Dim Path as String = "C:\...whatever you want" (Which path do I insert here? The main txt file?)


    also here:

    Dim AllLines() As String = File.ReadAllLines(Path) -> (What do I declare 'File' as?)

    I get the gist of everything else, thanks!

  11. #11
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: I need some help...

    Dim Path as String = "C:\...whatever you want" (Which path do I insert here? The main txt file?)
    You pass the path of the text file in that string. It even says it. That includes the text file's name.

    Dim AllLines() As String = File.ReadAllLines(Path) -> (What do I declare 'File' as?)
    I don't think you're even trying -_-

    When an error occurs in the IDE, you should always hover over it to see what it says. In this case, after hovering over it, a red exclamation mark will appear. Click on it and it will tell you the issue and even give you the option on automatically fixing it.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  12. #12
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: I need some help...

    Quote Originally Posted by truekoa View Post
    Ah I'm sorry I don't really understand the code...

    Dim Path as String = "C:\...whatever you want" (Which path do I insert here? The main txt file?)


    also here:

    Dim AllLines() As String = File.ReadAllLines(Path) -> (What do I declare 'File' as?)

    I get the gist of everything else, thanks!
    Path is a string variable that holds the path and name of the file you want to read the contents of(your file with customer names). So it would contain something like C:\TestInfo\Customers.txt. I said "whatever you want" since I don't know the path and name of file you are using.

    You don't declare File as anything. File is a class with a shared method called ReadAllLines and does as it says, reads all the lines of the file and returns the contents in a string array called AllLines. When you use a shared method as opposed to instance methods, you don't have to instantiate the class, just use its name directly with a dot and then the method name.

    Let me know if you have any other questions.
    Remember to click on the scales to the left and rep me if I helped

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Location
    Houston, TX
    Posts
    26

    Re: I need some help...

    Quote Originally Posted by weirddemon View Post
    You pass the path of the text file in that string. It even says it. That includes the text file's name.



    I don't think you're even trying -_-

    When an error occurs in the IDE, you should always hover over it to see what it says. In this case, after hovering over it, a red exclamation mark will appear. Click on it and it will tell you the issue and even give you the option on automatically fixing it.
    Sorry I'm really new to this lol. I took your advice and solved some of the errors with the suggestions..

    Quote Originally Posted by The Fire Snake View Post
    Path is a string variable that holds the path and name of the file you want to read the contents of(your file with customer names). So it would contain something like C:\TestInfo\Customers.txt. I said "whatever you want" since I don't know the path and name of file you are using.

    You don't declare File as anything. File is a class with a shared method called ReadAllLines and does as it says, reads all the lines of the file and returns the contents in a string array called AllLines. When you use a shared method as opposed to instance methods, you don't have to instantiate the class, just use its name directly with a dot and then the method name.

    Let me know if you have any other questions.
    Well I figured out most of the parts now. But when I go to:


    txtName.Text = PiecesOfLine(0)
    txtEmail.Text = PiecesOfLine(1)
    txtItem.Text = PiecesOfLine(2)
    txtTrack.Text = PiecesOfLine(3)
    txtSale.Text = PiecesOfLine(4)

    I keep getting an 'System.IndexOutOfRangeException' error with the "Piecesofline" not (0), but (1)-(5).

    Does this have to do with my txt file?

    This is what is in the txt file:
    (this is John Test.txt) John Test, [email protected], 123456789, 987654321, $250.00

    The other txt file is 'Client Database.txt'
    It just says John Test.txt

    What am I doing wrong?

    Thanks !
    Last edited by truekoa; Apr 17th, 2010 at 12:12 AM.

  14. #14
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: I need some help...

    Yes, it depends on the format of the line you are splitting with the split function. If the line has only 2 pieces of data with commas separating them, then you only have indecies 0 and 1. If you go to access index 2, you will get the error mentioned above. You cannot access a piece you don't have. You will need to determine how you want to do this. You could make your life easy and keep a standard file format, where all values are separated with a comma, whether they exist or not. For example you could have a line like this: John, [email protected],, 884848,9494949

    Notice that the 3rd field has no value but you are accounting for it with a comma. This way each line of the file has the same number of pieces whether there is info or not. This helps your split function, since you always know you have x number of pieces and you won't go out of bound in the array. You just need to know which piece represents which piece of data and put it into the appropriate textbox.
    Remember to click on the scales to the left and rep me if I helped

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Location
    Houston, TX
    Posts
    26

    Re: I need some help...

    I sorted it out, its working! lol.

    The reason I had problems with the System.IndexOutOfRangeException error is because I was still looking at the John Test.txt file because in my other coding I had Client Database.txt search thru John Test.txt for the info, but I rearranged my code to what you had suggested so there is only Client Database.txt now. I used the list format seperated by commas and it worked like a charm.

    Thanks alot!

  16. #16
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: I need some help...

    Quote Originally Posted by truekoa View Post
    I sorted it out, its working! lol.

    The reason I had problems with the System.IndexOutOfRangeException error is because I was still looking at the John Test.txt file because in my other coding I had Client Database.txt search thru John Test.txt for the info, but I rearranged my code to what you had suggested so there is only Client Database.txt now. I used the list format seperated by commas and it worked like a charm.

    Thanks alot!
    Great. Please mark this thread as resolved. Thanks.
    Remember to click on the scales to the left and rep me if I helped

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