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: -
Dim out As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("Agents.csv", True)
-
out.WriteLine(txtAgID.Text & "," & txtFName.Text & "," & txtLName.Text & "," & txtHDate.Text & "," & txtTDate.Text & "," & txtSup.Text & "," & txtShift.Text & "," & txtBRank.Text)
-
out.Close()
-
I have an identical userform that retrieves the data, the code looks like this: - Dim myLine As String
-
myLine = txtAgID.Text
-
Dim sr As System.IO.StreamReader
-
sr = System.IO.File.OpenText("Agents.csv")
-
sr.ReadLine()
-
-
Dim values As String()
-
While sr.Peek() >= 0
-
values = sr.ReadLine().Split(",")
-
If values(0) = myLine Then
-
-
txtFName.Text = values(1)
-
txtLName.Text = values(2)
-
txtHDate.Text = values(3)
-
txtTDate.Text = values(4)
-
txtSup.Text = values(5)
-
txtShift.Text = values(6)
-
txtBRank.Text = values(7)
-
-
Exit While
-
Else
-
lblStatus.Text = "Agent is not found in the database"
-
End If
-
End While
-
lblStatus.Text = "Record Located"
-
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: - Dim myLine As String
-
myLine = txtAgID.Text
-
Dim sr As System.IO.StreamReader
-
sr = System.IO.File.OpenText("Agents.csv")
-
-
sr.ReadLine()
-
-
Dim values As String()
-
While sr.Peek() >= 0
-
values = sr.ReadLine().Split(",")
-
If values(0) = myLine Then
-
values(1) = txtFName.Text
-
values(2) = txtLName.Text
-
values(3) = txtHDate.Text
-
values(4) = txtTDate.Text
-
values(5) = txtSup.Text
-
values(6) = txtShift.Text
-
values(7) = txtBRank.Text
-
-
Exit While
-
Else
-
lblStatus.Text = "Agent is not found in the database"
-
End If
-
End While
-
lblStatus.Text = "Record updated"
-
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.
7 6208
At no point in your code do you write anything.
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.
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.
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.
Eh, nvm, I got it to work by doing this: - Dim i As Integer
-
lblStatus.Text = ""
-
Dim oExcel As Object = CreateObject("Excel.Application")
-
Dim oBook As Object = oExcel.Workbooks.Open("C:\Users\owner\Desktop\EWFM\EWFM\EWFM\bin\Debug\Agents.csv")
-
Dim oSheet As Object = oBook.Worksheets(1)
-
On Error Resume Next
-
oExcel.DisplayAlerts = False
-
For i = 2 To 50000
-
If oSheet.Range("A" & i).value = txtAgID.Text Then
-
oSheet.Range("B" & i).value = txtFName.Text
-
oSheet.Range("C" & i).value = txtLName.Text
-
oSheet.Range("D" & i).value = txtHDate.Text
-
oSheet.Range("E" & i).value = txtTDate.Text
-
oSheet.Range("F" & i).value = txtSup.Text
-
oSheet.Range("G" & i).value = txtShift.Text
-
oSheet.Range("H" & i).value = txtBRank.Text
-
Exit For
-
Else
-
End If
-
Next
-
oBook.Close(SaveChanges:=True)
-
oExcel.Quit()
-
ReleaseComObject(oExcel)
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.
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,three","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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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
|
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,...
|
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...
|
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...
|
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
|
by: Harvey Triana |
last post by:
Hi-
I am using...
WebClient Client = new WebClient ();
Client.DownloadFile(url, filename);
Or
Stream strm = Client.OpenRead (url);
|
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;...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
| |