Read new record from CSV textfile and item not found

  • Thread starter Thread starter Ray
  • Start date Start date
R

Ray

Hello World,

I made a Windowsform that reads data from a CSV file.
It works fine, but when I have read the data of a record I have to re-Debug
the form to read another record.
So when I put a new seek item in the textbox after I searched for an item
and click the seek button.. it seems it is doing nothing.

And also... what code can I use to genereate a messagebox message when a
seek item is not at all in the CSV file?

If ?????????? Then
MessageBox.Show("Item is not in the file..")
End If


Thanks for reading this

Ray
 
You said it works fine, but you have to debug to read another record? That
doesn't really sound like "works fine" to me. ;-)

If you want help with reading the file, you need to post your code. If your
code is huge, make a small example and post that. Please post it in text
format, don't copy it from Visual Studio and paste it into an
OutlookExpress message.

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
Hello Robin,

Thanks for reacting. You're right. It doesn't work fine, but does some
things good..;-)
The main thing is OK: read the required data from a record and put them in
the right textbox...
But the.. read another record.. no way. Must restart the program again
first.

Ray

Here's the code: (read from 2 files and soe Dutch language)

Imports Microsoft.VisualBasic.FileIO

Public Class Form1

Dim objparser As New TextFieldParser("o:\cmdb\data\imdata.csv")
Dim Personeel As New TextFieldParser("o:\cmdb\data\vbatl.csv")

Dim conf As String
Dim atl As String

'Na een klik op de button worden de records uitgelezen



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnZoek.Click
objparser.Delimiters = New String() {";"}
Personeel.Delimiters = New String() {";"}

Me.Button1.BackColor = Color.LightSkyBlue

conf = (TextBox1.Text)
atl = (txtOwner.Text)

If TextBox1.Text = "" Then
MessageBox.Show("Verplicht veld!", "Invoerfout",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If

Do While Not objparser.EndOfData
Dim strvelden() As String = objparser.ReadFields




'MessageBox.Show("Laptop " & strvelden(0) & " wordt gebruikt
door " & strvelden(3) _
'& " " & strvelden(2), strvelden(1))




If conf = strvelden(5) Or conf = strvelden(1) Then

lblLogical.Text = strvelden(1)
txtSnr.Text = strvelden(5)
txtOwner.Text = strvelden(3)
txtOS.Text = strvelden(6)
txtSoort.Text = strvelden(0)
txtType.Text = strvelden(4)
txtScan.Text = strvelden(7)
txtLogonuser.Text = strvelden(8)


End If


' ' Do While Not Personeel.EndOfData
' Dim mensen() As String = Personeel.ReadFields

' If atl = mensen(0) Then
' txtMedewerker.Text = mensen(2) & " " & mensen(1)
' End If
'Loop
Loop


End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub




Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click


If TextBox1.Text = "" Then
MessageBox.Show("Geen serienummer of Computernaam ingevoerd",
"Invoerfout", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
Else
Do While Not Personeel.EndOfData
Dim mensen() As String = Personeel.ReadFields

If mensen(0) = txtOwner.Text Then
txtMedewerker.Text = mensen(2) & " " & mensen(1)
txtAfdeling.Text = mensen(4)
txtTelefoon.Text = mensen(3)
End If
Loop
End If
TextBox1.Text = ""
End Sub


Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
lblLogical.Text = ""
txtSnr.Text = ""
txtOwner.Text = ""
txtOS.Text = ""
txtSoort.Text = ""
txtType.Text = ""
txtScan.Text = ""
txtLogonuser.Text = ""
txtMedewerker.Text = ""
txtAfdeling.Text = ""
txtTelefoon.Text = ""
Me.Button1.BackColor = Color.LightGray
TextBox1.Focus()
End Sub
End Class
 
I don't understand why it doesn't work. I wrote a small test program, which
does work. See if you can figure out what's different between mine and
yours. The only thing i see if that I am setting the TextFieldType to
Delimited -- maybe if you try that, it will help.

I even tried instantiating my TextFieldParser like you did, instead of
using a Using clause, and it still worked.

Are you sure your file has carriage-return/line-feeds in it? Have you
checked it out in a binary/hex editor like Textpad? If you just try to read
it using ReadAllLines does it split it into lines?

Are you closing your textfieldparser at the end (this wouldn't affect your
not-being-able-to-read-the-lines, I'm just asking).

Here's my code:

Public Sub TestReadTabDelim()
Dim count As Integer = 0
Dim strFieldName As String = "c:\testDelim.txt"
Using MyReader As New FileIO.TextFieldParser(strFieldName)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {vbTab}
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt,
'report an error and continue parsing.
Do While Not MyReader.EndOfData And count < 50
count += 1
Try
currentRow = MyReader.ReadFields()
Console.WriteLine("Col 1 = {0}, Col 2 = {1}, Col3 = {2}", _
currentRow(0), currentRow(1), currentRow(2))
Catch ex As FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
Loop
End Using
End Sub

HTH,
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-------------------------------------------------------------
 
Back
Top