Okay, guys, I've been at this for hours and what is surely a simply answer has eluded me! :P

Here's my situation:

It's a list of computer systems, basically, that the user can purchase.

I need to read this file, line-by-line, into an array. Let's call this array csvline().

The first line of the text file would stored in csvline(0). Line two would be stored in csvline(1). And so on. (I've started with zero because that's where VB starts its arrays). A drop-down list would then enable the user to select 1, 2 or 3 (or however many lines/systems are stored in the file). Upon selecting a number - say, 1 - csvline(0) would be displayed inside a textbox (textbox1, let's say). If 2 was selected, csvline(1) would be displayed, and so on.

It's not the formatting I need help with, though; that's the easy part. I just need someone to help teach me how to read a CSV file line-by-line, putting each line into a string array - csvlines(count) - then increment count by one so that the next line is read into another slot.

So far, I've been able to paste the numbers of each system into an combobox:

Using csvfileparser As New Microsoft.VisualBasic.FileIO.TextFieldParser _
        ("F:\folder\programname\programname\bin\Debug\systems.csv")

            Dim csvalue As String()

            csvfileparser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            csvfileparser.Delimiters = New String() {","}

            While Not csvfileparser.EndOfData

                csvalue = csvfileparser.ReadFields()

                combobox1.Items.Add(String.Format("{1}{0}", _
                                          Environment.NewLine, _
                                          csvalue(0)))

            End While

        End Using

But this only selects individual values. I need to figure out how selecting one of these numbers in the combobox can trigger textbox1 to be appended with just that line (I can handle the formatting, using the string.format stuff). If I try to do this using csvalue = csvtranslator.ReadLine , I get the following error message:

"Error 1 Value of type 'String' cannot be converted to '1-dimensional array of String'."

If I then put it as an array, ie: csvalue() = csvtranslator.ReadLine , I then get a different error message:

"Error 1 Number of indices is less than the number of dimensions of the indexed array."

What's the knack, guys? I've spent hours trying to figure this out.

Please go easy on me - and keep any responses ultra-simple for my newbie brain - I'm very new to all this programming malarkey and just starting out! :)

Recommended Answers

All 3 Replies

Hi.
First of all, let me point out that I'm not familiar with the 'Microsoft.VisualBasic.FileIO.TextFieldParser' so my suggestion may not work for you.

Personally, I would read the entire text file into a single String Variable (let's call it myCSVfile) and then use the Split command to populate an array. Something along the lines of:

Dim csvLine As String, myCSVfile As String, myCSVarray() As String
Dim sr As New StreamReader('path to text file here')
Do
  csvLine = sr.ReadLine()
  myCSVfile = myCSVfile & csvLine
Loop Until csvLine Is Nothing
sr.Close
myCSVarray = Split(myCSVfile,", ")

Once you have the array you can populate your combo according to the number of entries in the array and then, when a user selects an item in the combo, use it's index value to select the corresponding array entry for your text box.

I hope this is of some help,

Chris.

>Reading individual lines of a CSV into a string array?

Try using System.IO.File.ReadAllLines("file.txt") method.

Thanks for the correction adatapost, seems I had my head screwed on backwards (thinking about CSV files).

Thanks for the System.IO method too.

Chris.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.