473,836 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB.NET - Read CSV file, look for specific data

20 New Member
Here is the deal:

I have a basic VB.NET (2012) app that I have coded to look for a specific CSV file. But that is where it stops cause I am stuck.

what I need to happen is when I click a button, I want a window to pop up for me to locate the CSV file.

THEN, I need my app to process said CSV file looking for a keyword. In this case: User_Licenses
Once it finds it, It needs to count the TRUE's below it till it hit an empty cell and populate a label with the number. (See image below for the CSV sample)

Mar 6 '13
27 10203
94 New Member
I looked at your file an you're getting the error because you're working with a screwed up CSV file. Whoever the developer is who wrote the code to output this should receive a very hard slap on their hand. Take a look at http://en.wikipedia.org/wiki/Comma-s...s_and_examples for best practices with CSV files. I work with banks and their back end systems and see this a lot. You basically have a CSV file with combined table outputs which makes it difficult to read and requires some fairly tricky code to handle it elegantly. So enough now with my rant. All you need to do is change the line:
Expand|Select|Wrap|Line Numbers
  1. ElseIf Not startCounting AndAlso csvFields.Length => 23 AndAlso csvFields(23) = "US_VoiceMailOn" Then
Mar 8 '13 #21
20 New Member
AND this isn't the only thing they screw up. :-)

Thanks, I will try that.
Mar 8 '13 #22
20 New Member
Ok, I tried that and got the same error.

Expand|Select|Wrap|Line Numbers
  1.         For Each line As String In System.IO.File.ReadAllLines("C:\Configuration.csv")
  2.             csvFields = line.Split(",")
  3.             If startCounting Then
  4.                 If csvFields(0) = String.Empty Then
  5.                     Exit For
  6.                 ElseIf csvFields(23) = "TRUE" Then
  7.                     trueCount += 1
  8.                 End If
  9.             ElseIf Not startCounting AndAlso csvFields.Length >= 23 AndAlso csvFields(23) = "US_VoiceMailOn" Then
  10.                 startCounting = True
  11.             End If
  12.         Next
  13.         Label4.Text = String.Format("{0} Users Licensed", trueCount)
Mar 8 '13 #23
94 New Member
Looks like the record which follows the section your looking for only has a single field. Change the following:
Expand|Select|Wrap|Line Numbers
  1. If csvFields(0) = String.Empty Then
  2. to
  3. If csvFields(0) = String.Empty OrElse csvFields.Length < 23 Then
Mar 8 '13 #24
20 New Member
That seemed to fix the issue with the error. But now, it only brings back 0.
Mar 8 '13 #25
94 New Member
I ran the following code against your CSV file and receive the following result:
#35 True Values
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  2.         Dim startCounting As Boolean = False
  3.         Dim trueCount As Integer = 0
  4.         Dim csvFields() As String
  5.         For Each line As String In System.IO.File.ReadAllLines("C:\Configuration.csv")
  6.             csvFields = line.Split(",")
  7.             If startCounting Then
  8.                 If csvFields(0) = String.Empty OrElse csvFields.Length < 23 Then
  9.                     Exit For
  10.                 ElseIf csvFields(23) = "True" Then
  11.                     trueCount += 1
  12.                 End If
  13.             ElseIf Not startCounting AndAlso csvFields.Length >= 23 AndAlso csvFields(23) = "US_VoiceMailOn" Then
  14.                 startCounting = True
  15.             End If
  16.         Next
  17.         MessageBox.Show(String.Format("#{0} True Values", trueCount))
  18.     End Sub
Mar 8 '13 #26
20 New Member
I am not sure what I was doing but I copied that code into my app and it works. Thanks man. I appreciate your teaching. I really do.
Mar 8 '13 #27
94 New Member
No problem! I've been in your situation and received a lot of help so and I've come to the point where it's time for me to pay it back.
Mar 8 '13 #28

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Shelley | last post by:
Hi everyone, I am searching a way to read part of the data in the text field in a form. As the length of the field is dynamic, I can't use the left or right function. I need to read the first 2 words from the specific control like: Get "January 2005" from "January 2005 final", or "January 2005 test samples", what I need is the first two words. What function I can use?
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
by: Profetas | last post by:
I have the following code that detects a <c> and </c> #include <stdio.h> main(int argc, char *argv) { FILE* fp; char data;
by: David Warner | last post by:
Greetings! I am working on a C app that needs to read print files generated by lp that contain HP LaserJet PCL codes. If the PCL contains binary data to define a bit map to be printed on the page, either icon or soft-font download, the EOF test ends prematurely and does not read the entire file. How do I get around testing for EOF and not detect the EOF value within the file? Am I missing something? More Data:
by: computerwolf8 | last post by:
I have a file where I know the lines go as follows: string long string int int string double
by: somequestion | last post by:
During copying file , wanna read file Size like this string CheckFileSize(string fileName) { if( fileName == null ) return; FileInfo fi = new FileInfo(fileName); return fi.Length.ToString(); } but it shows total file size...
by: xplode144 | last post by:
I have a Web application. i need to read a file once during the startup and preserve the read data throughout the life of the application. i will to access the data often during the page_load of the default.aspx page. what is the best way to do so? do i need to implement a singlton object?
by: lazyman | last post by:
I'm having a problem getting just the data from the file I want. I can get the file to read in to a msflexgrid but I can't get the data out of the cells I want is there an easier way to read a csv for data? Thanks in advance.
by: ma740988 | last post by:
I'm perusing a question and curiosity got the best of me in part because I cant solve the problem Consider: typedef unsigned int word_type ; class foo { public: void set_mx_digits_1 ( word_type wt ) {}
Mas Juliza Alias
by: Mas Juliza Alias | last post by:
Hi, I am building a program on Information System for Reservoir Operation for my Masters research. Now I have a text file which is an output file from a processing software (Terramodel) that lists out Elevation Range and Volume in columns and rows. The text file looks like this; ------------------------------------------------- -software's distributor address- -project description- Depth range Volume (m) ...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.