Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

opening an updating a text file

Status
Not open for further replies.

leearach2004

Technical User
Dec 8, 2005
86
GB
Hi there ive just started to play around with using data in text files.

Have have got the view data in a text file working in the OnPage_Load Routine but I have a button to update the text file with the code to write information to the file.

The problem I have is the first time you click the button nothing happens, but then every time after that it works fine.

This is the basic test code i am using for this

sub Page_Load(sender as Object, e as EventArgs)
'Open a file for reading
Dim FILENAME as String = Server.MapPath("test.txt")

'Get a StreamReader class that can be used to read the file
Dim objStreamReader as StreamReader
objStreamReader = File.OpenText(FILENAME)

'Now, read the entire file into a string
Dim contents as String = objStreamReader.ReadToEnd()

'Set the text of the file to a Web control
TextBox2.Text = contents

objStreamReader.Close()
end sub

Sub btnupd_Click(sender As Object, e As EventArgs)

'Open a file for reading
Dim FILENAME as String = Server.MapPath("test.txt")

'Get a StreamReader class that can be used to read the file
Dim objStreamWriter as StreamWriter
objStreamWriter = File.AppendText(FILENAME)

'Now, write the contents of textbox1 to text file
objStreamWriter.Write(vbCrLf)
objStreamWriter.Write(TextBox1.Text)

'Close the stream
objStreamWriter.Close()

End Sub

I just dont understand why it wont work on first click but will evertimne after

hope somone can help

lee
 
lee: try this

Right after your page load put in the following:
Code:
Sub Page_Load(sender as Object, e as EventArgs)
  If Not Page.IsPostBack Then
   'code here
  End If
End Sub
In this way you'll avoid the intial page load event on a postback for handling the button event.
 
Hi there thanks for that it worked great, thats what i was trying to figure out just mined through a blank thanks alot for your help

lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top