Buiilding a program to search a csv file by partnumber, then recover the info associated with that partnumber

I have a csv file with several partnumbers, descriptions, weight, cost, etc.. I have code to find a particular partnumber but can't figure out how to get the info that goes with that partnumber like cost, weight, etc. Here is the code so far:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim FiletoSearch As String
        FiletoSearch = c:\csvfiles\partnumbers.csv
        Dim Findstring = IO.File.ReadAllText(FiletoSearch)
        Dim wordSearch As String

        wordSearch = TextBox1.Text

        If Findstring.Contains(wordSearch) Then
            MsgBox("Found: " & wordSearch)

        Else
            MsgBox(wordSearch & " Not There")
        End If

    End Sub

Each partnumber, lets say 15133, has a string of data with it and the last value in the string is 15133.jpg. I want to capture all between the partnumber 15133 and the end of all its information at 15133.jpg. How is this done?

Thanks
nucomputerguyAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Try something like this.

If foundLine.Count() = 0 Then
    '' Display not found message to user
Else
     '' the list(Of String) has each of the fields of the csv file. Index zero of the list is the first field of of the line from the csv line. And so on.
     TextBox1.Text = foundLine(0)
     TextBox2.Text = foundLine(1)
      ' continue this till all data is displayed
End If

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi nucomputerguy;

Modified the code to get the results you want.

'' Looking for this part number
Dim FiletoSearch As String
FiletoSearch = "c:\Working Directory\Test.csv"
'' Load the file into a List(Of String), each element in the list is one line in the file
Dim Findstring As List(Of String) = IO.File.ReadAllLines(FiletoSearch).ToList()
'' Search for an item
Dim wordSearch As String

wordSearch = TextBox1.Text
'' When the For Each loop completes this will hold all the columns
'' of the row the wordSearch was found in.
Dim foundLine As New List(Of String)()

'' Find the search word
For Each line As String In Findstring
    If line.Contains(wordSearch) Then
        '' Found the search word and storing it in the list
        foundLine.AddRange(line.Split(New String() {","}, StringSplitOptions.None).ToList())
        '' Exit because we have completed the search and found the word
        Exit For
    End If
Next

If foundLine.Count() = 0 Then
    Console.WriteLine("Part number " & wordSearch & " was not found")
Else
    '' Do what you need to do with the columns of that row
End If

Open in new window

0
 
nucomputerguyAuthor Commented:
I'm missing something. Was wanting to put the found part number and its data in a textbox. Not using console. A windows form application.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Fernando SotoRetiredCommented:
That was a Windows Form application I posted note line 9 where the data is coming from.

Console.WriteLine(...) are being used here to test the data to the IDE Output put window. Please remove those lines of code and replace them with what you want to display them at.
0
 
nucomputerguyAuthor Commented:
I'm sorry Mr Soto but I don't know how to replace them with what I want to display them at. I am new to Visual Studio 2013 from VB6. Was hoping to put the found partnumber and its information from its columns in a text string and the text string visible in a textbox.
0
 
nucomputerguyAuthor Commented:
There we go! Thanks for the code. Can take it from here. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.