473,416 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Deleting data from a file

Hi there,

I'm using the code below to try to delete a name from a list of names in a
file.

Unfortunately it doesn't quite do what I want it to.

Instead of looking for the name in the textbox and deleting that it is just
deleting the first line of data in the file.

It also writes that data back to the new file in a different format to how
it was originally ie instead of having the lines "jo bloggs, 094352678
john smith,094563728"
I get "jo bloggs, 094352678,john smith,094563728"

What must I adjust in this code to get it to do what I need (delete the name
that is shown in the textbox and display it in the file in the first way
shown above).

Cheers,

Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btndelete.Click
Dim message As String
If txtname.Text <> "" Then
If IO.File.Exists(TextBox1.Text) Then
deleteperson()
Else
message = "Either no file has yet been created or "
message = message & "the file cannot be found."
MsgBox(message, , "File not found.")
End If
End If
End Sub
Sub deleteperson()
Dim name, phoneno As String
Dim data() As String
Dim foundflag As Boolean = False
Dim sr As IO.StreamReader = IO.File.OpenText(TextBox1.Text)
Dim sw As IO.StreamWriter = IO.File.CreateText("temp.txt")
data = sr.ReadLine().Split(","c)
name = data(0)
phoneno = data(1)
Do While (sr.Peek <> -1)
name = sr.ReadLine
phoneno = sr.ReadLine
If (name <> txtname.Text) Then
Dim outputline() As String = {name, phoneno}
sw.WriteLine(Join(outputline, ","))
Else
foundflag = True
End If
Loop
sr.Close()
sw.Close()
IO.File.Delete(TextBox1.Text)
IO.File.Move("temp.txt", TextBox1.Text)
If Not foundflag Then
MsgBox("The name was not found.", , "")
Else
txtname.Clear()
End If
End Sub

Nov 23 '05 #1
2 1693
Hi,

Why are you not just using the indexof method from string in the line that
you did read and than don't write the line back if the index is not zero

if line.indexOf("John") <> then
' write the line
end if

Have as well a look at the reading of the streamreader, that peek is not
standard for a streamreader in normal text file reading.

http://msdn.microsoft.com/library/de...xtFromFile.asp

Cor
Nov 23 '05 #2
SiouxieQ wrote:
You have a few confusing things in your code:
data = sr.ReadLine().Split(","c)
name = data(0)
phoneno = data(1)
Here, you read a line from your file, split it apart based on a comma
and assign data(0) to name, and data(1) to phone.
Do While (sr.Peek <> -1)
name = sr.ReadLine
phoneno = sr.ReadLine


But right here inside the while loop, you discard those values and read
two more lines? What is the structure of the file? As Cor suggested,
if you do a ReadLine and then use the String.IndexOf method to search
for the name, that would probably produce cleaner code.

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Baker | last post by:
Hi: I am working with an XLS file, andhave Access 2000. When I try and clear the xlf file (so that xls can use it for additional data), I get an error message: "Deleting data in a linked...
13
by: Bob Darlington | last post by:
I have a repair and backup database routine which runs when a user closes down my application. It works fine in my development machine, but breaks on a client's at the following line: If...
3
by: kris.dorey | last post by:
Hi, Ive got the following code which seems ok but when the user runs the function for a second time I get an error message stating that the mdb is in use by another process. There is still an...
2
by: melanieab | last post by:
Hi, I'm deleting nodes in my xml file, and it does seem to work, but then when I later reload the file and make an xmlNodeList, the nodelist count still includes the deleted nodes yet the file...
2
by: GMK | last post by:
Dear all in my asp.net application i have a text file that is installed with my application on the server. this text file is filled with data through a web interface in my application. i need to...
8
by: shandra | last post by:
I have a file I need to delete or truncate. I tried using the KILL command in VB6. I tried using the file.delete command in VB.net. I tried manually deleting, renaming, and copying over the...
0
by: John Yale | last post by:
I am using an XmlDocument to save some data. Periodically I add new data and save the XmlDocument to a file: Me.fXmlDoc.Save(Me.fFilename) When I have added all the data I need, I want to move...
2
by: emphyrio | last post by:
Hi, I am new to programming in vb. net. I have this problem: I created a form with a datagrid. I can add records and then save them to a XML file, using this code on the click event of a button...
4
by: MiziaQ | last post by:
Hey, I'm using the following code to write entries to a data file and then read them in an msflexgrid. I now would like to add code under a delete button to use the table(grid) to delete rows from...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
isladogs
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...

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.