i am using VB.NET 2003 and i need to read some data from a cvs file and store it into a two dimensional array.... i couldn't find an example anywhere.. pls help :'(

Recommended Answers

All 7 Replies

See How to read csv file contents in VB.Net. It reads a csv file lines in to an array. There's comment at the end, how to get (comma) separate values from one line. Use the code in that thread and add a second loop to split lines and finally put data in to a two-dimensional array.

I won't post any code now. Try to do it first by yourself. If you encounter a problem, post your code and I'll help you. It's not that difficult task ;)

Public Class FormStart

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim readText() As String = System.IO.File.ReadAllLines("C:\file.txt")
		Dim s As String
		For Each s In readText
			Dim columns() As String = s.Split(New [Char]() {","c})
			System.Diagnostics.Debugger.Break()
		Next
	End Sub
End Class

The OP wanted csv file to a matrix. Here's sknake's code slightly modified

Dim Matrix(,) As String

Dim readText() As String = System.IO.File.ReadAllLines("C:\file.txt")
' Redim matrix
ReDim Matrix(readText.GetUpperBound(0), Strings.Split(readText(0), ",").GetUpperBound(0))

Dim s As String
Dim Line As Integer = 0
Dim j As Integer
For Each s In readText
  Dim columns() As String = s.Split(New [Char]() {","c})
  ' Copy one line to matrix
  For j = 0 To columns.GetUpperBound(0)
    Matrix(Line, j) = columns(j)
  Next j
  Line += 1
  System.Diagnostics.Debugger.Break()
Next

The code works with "well-formed" csv files but crashes if csv file has some oddities. There should be bounds checking added to the code.

i have used the code that you have provited but it gives me an errror... it sais: ReadAllLines is not a member of 'System.IO.File'

i have used the code that you have provited but it gives me an errror... it sais: ReadAllLines is not a member of 'System.IO.File'

Use System.IO.StreamReader class.

What version of the framework are you using? That method was implemented in 2.0 so if you are using 1.x then you probably need another approach as adatapost suggested

i am using VB.NET 2003

Sorry, my mistake. Here's 1.1 compatible version

Dim Matrix(,) As String

' .NET 2.0
'Dim readText() As String = System.IO.File.ReadAllLines("C:\file.txt")
' .NET 1.1
Dim readText() As String
Dim OneLine As String
Dim Lines As Integer
Lines = 0
Dim oReader As StreamReader = New StreamReader("C:\file.txt")
OneLine = oReader.ReadLine
Do Until OneLine Is Nothing
  ReDim Preserve readText(Lines)
  readText(Lines) = OneLine
  Lines += 1
  OneLine = oReader.ReadLine
Loop

' If matrix is empty, exit sub in here

' Redim matrix
ReDim Matrix(readText.GetUpperBound(0), Strings.Split(readText(0), ",").GetUpperBound(0))

Dim s As String
Dim Line As Integer = 0
Dim j As Integer
For Each s In readText
  Dim columns() As String = s.Split(New [Char]() {","c})
  ' Copy one line to matrix
  For j = 0 To columns.GetUpperBound(0)
    Matrix(Line, j) = columns(j)
  Next j
  Line += 1
  System.Diagnostics.Debugger.Break()
Next
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.