|
-
Mar 3rd, 2011, 08:45 AM
#1
Thread Starter
New Member
[RESOLVED] help with csv files and arrays
Hi all, so im new to VB.net, What I need to do is open a csv file which contains 4 fields and many rows, then I want to do different operations determined on the value in the fields.
for example :
My CSV will be in the following format.
ID,Name,Number,Status
Heres what I have so far
Code:
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
Dim filestream As StreamReader
filestream = File.OpenText(OpenFileDialog1.FileName.ToString())
Dim readcontents As String
Dim counter As Integer
Dim Fails As Integer
Dim Delivered As Integer
Do Until filestream.EndOfStream
readcontents = filestream.ReadLine()
Dim textdelimiter As String
textdelimiter = ","
Dim splitout = Split(readcontents, textdelimiter)
counter = counter + 1
'Check the values of each field
Loop
filestream.Close()
strm.Close()
MessageBox.Show("counter:" & counter & "Records in file")
MessageBox.Show("Operation Complete")
End If
End Sub
My issue is that whilst in the loop I want to extract the data from the array and assign it to a variable but I don't know how to do this without receiving an out of bound exception ( I can do this simple task in perl and php but as I am new to VB.net I haven't the slightest idea how.
Could somebody point me in the right direction, I would appreciate it alot.
EDIT: - I want to perform an IF statement on each field of the csv.. e.g :
if status = 'good' then
do something
Else
do something else
Last edited by minimatrix; Mar 3rd, 2011 at 08:59 AM.
Reason: Missed out some information ( Now appended )
-
Mar 3rd, 2011, 10:16 AM
#2
Re: help with csv files and arrays
In place of this line:
Code:
'Check the values of each field
you could write something like this:
vb.net Code:
if splitout(0) = "good" then '~~~ Checks first part. ie., at index 0 of the array
'do something
else
'do something for false
end if
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Mar 3rd, 2011, 10:32 AM
#3
Thread Starter
New Member
Re: help with csv files and arrays
thanks for your reply!
apologies, I forgot to mention that this is what I was already attempting previously but I kept receiving an indexoutofbounds exception.
Please see below the code I had with this test in.
Code:
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
Dim filestream As StreamReader
filestream = File.OpenText(OpenFileDialog1.FileName.ToString())
Dim readcontents As String
Dim counter As Integer
Dim Fails As Integer
Dim Delivered As Integer
Do Until filestream.EndOfStream
readcontents = filestream.ReadLine()
Dim textdelimiter As String
textdelimiter = ","
Dim splitout = Split(readcontents, textdelimiter)
counter = counter + 1
If splitout(3) = "DELIVERED" Then
Delivered = Delivered + 1
ElseIf splitout(3) = "FAILED" Then
Fails = Fails + 1
End If
'Check the values of each field
Loop
filestream.Close()
strm.Close()
MessageBox.Show("counter:" & counter & "Records in file with " & Fails & " Failures and " & Delivered & " Delivered.")
MessageBox.Show("Operation Complete")
End If
End Sub
Regards
minimatrix
-
Mar 3rd, 2011, 10:46 AM
#4
Re: help with csv files and arrays
Here's how I tested it:
vb.net Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'~~~ A sample line from file
Dim tempFileContent As String = "123,Akhilesh,1,good"
'~~~ Splitting the string
Dim strArr() As String = tempFileContent.Split(","c)
'~~~ Checking it
If strArr(3).Trim.ToLower = "good" Then
MessageBox.Show("Wow ! You are in good condition..")
Else
MessageBox.Show("Hey... you are in bad condition")
End If
End Sub
End Class
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Mar 3rd, 2011, 11:48 AM
#5
Thread Starter
New Member
Re: [RESOLVED] help with csv files and arrays
Thanks for your help .. I realised that it was my input file that was incorrect and not the code .... my bad.
I really appreciate your help though.
This thread has been marked as resolved.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|