Link to home
Start Free TrialLog in
Avatar of David Walters
David Walters

asked on

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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

Avatar of David Walters
David Walters

ASKER

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.
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There we go! Thanks for the code. Can take it from here. :)