|
-
Apr 14th, 2010, 11:02 PM
#1
Thread Starter
Junior Member
[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.
-
Apr 14th, 2010, 11:02 PM
#2
Thread Starter
Junior Member
Re: I need some help...
What am I doing wrong?
PHP Code:
Public Class frmDatbase
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 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
-
Apr 14th, 2010, 11:05 PM
#3
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?
-
Apr 14th, 2010, 11:06 PM
#4
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
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Apr 14th, 2010, 11:15 PM
#5
Thread Starter
Junior Member
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?
-
Apr 14th, 2010, 11:34 PM
#6
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?
-
Apr 15th, 2010, 01:32 PM
#7
Thread Starter
Junior Member
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.
-
Apr 15th, 2010, 10:22 PM
#8
Thread Starter
Junior Member
Re: I need some help...
anyone? I just need a point in the right direction on my coding. Thanks
-
Apr 15th, 2010, 11:19 PM
#9
Hyperactive Member
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:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Path as String = "C:\...whatever you want"
Dim AllLines() As String = File.ReadAllLines(Path)
Dim PiecesOfLine() As String
For Each line In AllLines
' Check line to see if it matches the search criteria
If line.Contains(txtCustomer.Text) Then
' Line in file matches the search criteria so split it up
PiecesOfLine = line.Split(",")
' Fill in all the textboxes on the screen with the pieces
txtName.Text = PiecesOfLine(0)
txtemail.Text = PiecesOfLine(1)
' etc... Get the idea?
Exit For
End If
Next
End Sub
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 
-
Apr 16th, 2010, 11:02 PM
#10
Thread Starter
Junior Member
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!
-
Apr 16th, 2010, 11:12 PM
#11
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
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Apr 16th, 2010, 11:24 PM
#12
Hyperactive Member
Re: I need some help...
 Originally Posted by truekoa
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 
-
Apr 17th, 2010, 12:06 AM
#13
Thread Starter
Junior Member
Re: I need some help...
 Originally Posted by weirddemon
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..
 Originally Posted by The Fire Snake
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.
-
Apr 18th, 2010, 01:07 AM
#14
Hyperactive Member
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 
-
Apr 18th, 2010, 01:58 PM
#15
Thread Starter
Junior Member
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!
-
Apr 18th, 2010, 09:34 PM
#16
Hyperactive Member
Re: I need some help...
 Originally Posted by truekoa
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|