473,765 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Modify a CSV file

4 New Member
I am hoping someone can help me with this. I am trying to figure out how to update data in a csv file with changes I make in a userform. I use one form to create the csv file, the code looks like this:

Expand|Select|Wrap|Line Numbers
  1.         Dim out As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("Agents.csv", True)
  2.         out.WriteLine(txtAgID.Text & "," & txtFName.Text & "," & txtLName.Text & "," & txtHDate.Text & "," & txtTDate.Text & "," & txtSup.Text & "," & txtShift.Text & "," & txtBRank.Text)
  3.         out.Close()
  4.  
I have an identical userform that retrieves the data, the code looks like this:

Expand|Select|Wrap|Line Numbers
  1. Dim myLine As String
  2.         myLine = txtAgID.Text
  3.         Dim sr As System.IO.StreamReader
  4.         sr = System.IO.File.OpenText("Agents.csv")
  5.         sr.ReadLine()
  6.  
  7.         Dim values As String()
  8.         While sr.Peek() >= 0
  9.             values = sr.ReadLine().Split(",")
  10.             If values(0) = myLine Then
  11.  
  12.                 txtFName.Text = values(1)
  13.                 txtLName.Text = values(2)
  14.                 txtHDate.Text = values(3)
  15.                 txtTDate.Text = values(4)
  16.                 txtSup.Text = values(5)
  17.                 txtShift.Text = values(6)
  18.                 txtBRank.Text = values(7)
  19.  
  20.                 Exit While
  21.             Else
  22.                 lblStatus.Text = "Agent is not found in the database"
  23.             End If
  24.         End While
  25.         lblStatus.Text = "Record Located"
  26.         sr.Close()
I want to be able to change a record in one of those text boxes and have it save the change to the csv file. For example, if an agent's BidRank changes I want to update the existing record with the new BidRank. I tried this:
Expand|Select|Wrap|Line Numbers
  1. Dim myLine As String
  2.         myLine = txtAgID.Text
  3.         Dim sr As System.IO.StreamReader
  4.         sr = System.IO.File.OpenText("Agents.csv")
  5.  
  6.         sr.ReadLine()
  7.  
  8.         Dim values As String()
  9.         While sr.Peek() >= 0
  10.             values = sr.ReadLine().Split(",")
  11.             If values(0) = myLine Then
  12.                 values(1) = txtFName.Text
  13.                 values(2) = txtLName.Text
  14.                 values(3) = txtHDate.Text
  15.                 values(4) = txtTDate.Text
  16.                 values(5) = txtSup.Text
  17.                 values(6) = txtShift.Text
  18.                 values(7) = txtBRank.Text
  19.  
  20.                 Exit While
  21.             Else
  22.                 lblStatus.Text = "Agent is not found in the database"
  23.             End If
  24.         End While
  25.         lblStatus.Text = "Record updated"
  26.         sr.Close()
and it looks like it works (i.e. it pops no errors) but when I check the csv file it still has the old data. Any suggestions? Also, I am using Visual Studio.net 2008, forgot to mention this originally.
Feb 3 '13 #1
7 6247
Rabbit
12,516 Recognized Expert Moderator MVP
At no point in your code do you write anything.
Feb 4 '13 #2
pyrusane
4 New Member
I make the changes in the text box (for ex: txtBRank) and when I click the Update button it runs the code in the third box there. My goal is to set the lines in the array to the values in the text box but...I'm not sure how to do that. Tried using Streamwriter and Streamreader together and it did NOT like that. I should probably mention that I have a lot of experience doing stuff like this in VBA for Excel but very little in VB.net. I am mostly a novice with VB.net when it comes to anything more complex than a basic form or calculator.
Feb 4 '13 #3
Rabbit
12,516 Recognized Expert Moderator MVP
The general methodology in VBA would be the same in VB.net. In VBA, you would not attempt to write a file as you have done here. If you post the code for how you would do this in VBA, you will see where you went wrong in your logic.
Feb 4 '13 #4
pyrusane
4 New Member
Ok, I see what you're saying. I'm not actually changing the value in the csv, I'm just changing the variable so that it references the textbox instead of the csv. I could just use the same code I used to write the data to begin with, but then I would have a duplicate record with slightly different data. I don't know how to overwrite the data rather than appending a duplicate record.
Feb 4 '13 #5
pyrusane
4 New Member
Eh, nvm, I got it to work by doing this:
Expand|Select|Wrap|Line Numbers
  1.     Dim i As Integer
  2.         lblStatus.Text = ""
  3.         Dim oExcel As Object = CreateObject("Excel.Application")
  4.         Dim oBook As Object = oExcel.Workbooks.Open("C:\Users\owner\Desktop\EWFM\EWFM\EWFM\bin\Debug\Agents.csv")
  5.         Dim oSheet As Object = oBook.Worksheets(1)
  6.         On Error Resume Next
  7.         oExcel.DisplayAlerts = False
  8.         For i = 2 To 50000
  9.             If oSheet.Range("A" & i).value = txtAgID.Text Then
  10.                 oSheet.Range("B" & i).value = txtFName.Text
  11.                 oSheet.Range("C" & i).value = txtLName.Text
  12.                 oSheet.Range("D" & i).value = txtHDate.Text
  13.                 oSheet.Range("E" & i).value = txtTDate.Text
  14.                 oSheet.Range("F" & i).value = txtSup.Text
  15.                 oSheet.Range("G" & i).value = txtShift.Text
  16.                 oSheet.Range("H" & i).value = txtBRank.Text
  17.                 Exit For
  18.             Else
  19.             End If
  20.         Next
  21.         oBook.Close(SaveChanges:=True)
  22.         oExcel.Quit()
  23.         ReleaseComObject(oExcel)
Feb 4 '13 #6
Mikkeee
94 New Member
Pyrusane, you might want to reevaluate your csv technique. I once started down the path that you're on but there are many holes in your logic. A valid csv may contain embedded quotes and commas which are not intended to delimit the fields as long as they're embedded between quotes or properly escaped. I found a very nice csv reader which is super easy to use and extremely flexible. You should read the article at http://www.codeproject.com/Articles/...ast-CSV-Reader and decide for yourself. I'm not affiliated with the code in any way but I found it extremely fast and useful.
Feb 4 '13 #7
!NoItAll
297 Contributor
Mikkeee is right - the apparent simplicity of a CSV file is misleading. Fields in CSV files can, and often will, include quotes and commas. If you do not account for that then you will eventually miss-parse a file and likely corrupt it.
so the following is perfectly valid

Field one,Field two,"field,thre e","field,"four "",field five
The results of a proper parser will be

Field one
Field two
field,three
field, "four"
field five

The results of your parser will be
Field one
Field two
"field
three"
"field
"four""
field five


...that's a mess

While it's tempting to say, "well I can control and prevent users from entering quotes and commas in fields" that creates a code base that someone may inherit someday that will make him/her stick a pin in your voodoo doll.

Des
Feb 9 '13 #8

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

Similar topics

5
2065
by: vishal | last post by:
is there any way using which i can create .exe file of my php files. actually i am putting my php files on a web server and i don't want my files to be copied and modified by someone else/even not by administrator of web server. if i am putting php files then administrator has right to copy file and he may modify it as per his requirements.. if i put exe file then he can't modify file.
2
1346
by: Chris Cioffi | last post by:
Hello all, Are there any docs or examples of extending the file type? I work with EDI messages that are very like text files, just with a few quirks. ;-) and I was wondering if I could strech and twist the built in file type to make things a bit faster and more full featured. Specifically I would need to alter the iterator and ideally the line terminitor. Chris
13
3426
by: raykyoto | last post by:
Hi all, I'm sure this is a popular question that comes up every few months here. Indeed, I've looked at some of the past postings, but I would like to ask things differently. Basically, I'm using a flat file to storing data. I have to do this because mySQL is not installed on my web server, and I am not the root user. The amount of data is so small, that it isn't worth a full-blown database anyway. However, while the data is...
3
1850
by: ywchan | last post by:
I have a program that automatically generate a file and I need to put some information in the file properties. Any ideas? Thanks
2
4812
by: ywchan | last post by:
I want to access and modify the following file properties 1. when you open an application e.g. word / acrobat reader, those information in file -> properties. 2. when you right click the file, choose properties, the information stored in summary thanks
7
2052
by: Dennis C. Drumm | last post by:
I would like to be able to update an xml file located on my hosted server from my local computer. The server requires a user name and password to access the web site for writing or updating files. Could someone refer me to a doc that explains this or provide some sample code? Thanks,
2
1409
by: =?Utf-8?B?YW5rMmdv?= | last post by:
Thanks in advance for reading this. Let's say I have a file (file01) with this data in ASCII (ignore line col): line01 123abc line02 Header01 Starts blah var line03 detail01 000001 line04 detail02 000002 line05 detail03 000003 line06 detail04 000004
3
998
by: Darko Bazulj | last post by:
Hi, txt file. list listname1@domain.tld parameter1 value parameter2 value parameter3 value list listname2@domain.tld parameter1 value
4
4365
by: Harvey Triana | last post by:
Hi- I am using... WebClient Client = new WebClient (); Client.DownloadFile(url, filename); Or Stream strm = Client.OpenRead (url);
10
1927
by: arvindha | last post by:
I want to read a file from a point where a regular expression matches and keep on reading till i hit another regular expression; while reading this part of the file ( binary strings of known size; search the appropriate position, parsed through a fil, make a replacement);
0
9568
marktang
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...
0
9404
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,...
0
10164
Oralloy
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...
0
10007
jinu1996
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...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
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...
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.