StreamReader and text file problem

  • Thread starter Thread starter ArtySin
  • Start date Start date
A

ArtySin

I'm trying to display a text file on a web page and the code below works fine
and display the text on a webpage. However,if I use (as I need to) a
masterpage and then place the code in a webform that uses the masterpage all
it does is display the words 'Meeting minutes' which is the header for the
text right at the bottom of the code below. Is there something in a
masterpage that prevents this working at all?
Cheers
ArtySin

<%@ Import Namespace="System.IO" %>
<script language="vb" runat="server">
sub Page_Load(sender as Object, e as EventArgs)

Dim FILENAME as String = Server.MapPath("minutes.txt")

Dim objStreamReader as StreamReader
objStreamReader = File.OpenText(FILENAME)

Dim contents as String = objStreamReader.ReadToEnd()


lblRawOutput.Text = contents

objStreamReader.Close()
end sub
</script>

<b>Meeting Minutes</b><br />
<asp:label runat="server" id="lblRawOutput" />
 
The problem is the path is not resolved properly when master pages are used.
Try this:

Dim FILENAME as String = Server.MapPath("~minutes.txt")
 
No luck with that Scott. I tried:
(~minutes.txt")
(~/minutes.txt")
(~/pathname/minutes.txt")

but none of these works at all ..... any other ideas/
Cheers
ArtySin
 
ArtySin said:
No luck with that Scott. I tried:
(~minutes.txt")
(~/minutes.txt")
(~/pathname/minutes.txt")

but none of these works at all ..... any other ideas/

If you look at the source in the browser, has the text been put in there in
some way to make it invisible, e.g. stuck in a comment <!-- or similar?

Andrew
 
This is server-side code. It would not be rendered to the client and
therefore you wouldn't be able to see it.

The problem is that the path to the file to be read is being altered due to
the master page.
 
I've sorted it!
The first line should be:
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

All now works fine
ArtySin

*****************************************
 
Oops! I didn't even look at that line. Let me ask you something...did you
write that event handler yourself? You should NEVER have to write the stub
of an event handler if you are using Visual Studio. If you do write them
yourself, as you've seen, you can make mistakes with them.

-Scott
 
No, I didn't write this myself, I got the code from 4guysfromrolla.com and
they missed it out too
 
That's not quite what I mean. The fact is that you did put that line in
there (via copy/paste) and you should never do that with an event handler.
The 4guysfromrolla.com code is just fine, it's written to work in an
environment where AutoEventWireUp (page level directive) is set to True.

What I meant was that you (whether your "wrote" the code yourself or you
paste it in there) should NEVER be writing the first or last line of an
event handler procedure. You should always let Visual Studio do it for you.
In the case of a web page load event, all you need to do is double click on
the page designer surface and VS will take you to code view and create the
event handler stub ready for you to fill in.

By letting VS do this kind of work for you will eliminate many problems like
this one.

Good luck!
 
This is server-side code. It would not be rendered to the client and
therefore you wouldn't be able to see it.

Ummm... I was thinking that for some reason the data may have been inserted
into the rendered HTML but in a way that the browser didn't show it:
lblRawOutput.Text = contents
<asp:label runat="server" id="lblRawOutput" />

Of course, the OP should check for File.Exists before trying to read it.

Andrew
 
But again, server-side code doesn't render like that. An ASP .NET label
renders as an HTML <span> element.
 
Back
Top