473,386 Members | 1,883 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,386 software developers and data experts.

StreamReader and text file problem

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" />
Jun 27 '08 #1
10 1304
The problem is the path is not resolved properly when master pages are used.
Try this:

Dim FILENAME as String = Server.MapPath("~minutes.txt")
"ArtySin" <Ar*****@discussions.microsoft.comwrote in message
news:15**********************************@microsof t.com...
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" />


Jun 27 '08 #2
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

"Scott M." wrote:
The problem is the path is not resolved properly when master pages are used.
Try this:

Dim FILENAME as String = Server.MapPath("~minutes.txt")
"ArtySin" <Ar*****@discussions.microsoft.comwrote in message
news:15**********************************@microsof t.com...
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" />


Jun 27 '08 #3
ArtySin wrote:
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
Jun 27 '08 #4
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.

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:67*************@mid.individual.net...
ArtySin wrote:
>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

Jun 27 '08 #5
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

*****************************************

"Scott M." wrote:
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.

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:67*************@mid.individual.net...
ArtySin wrote:
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


Jun 27 '08 #6
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

"ArtySin" <Ar*****@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
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

*****************************************

"Scott M." wrote:
>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.

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:67*************@mid.individual.net...
ArtySin wrote:
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



Jun 27 '08 #7
No, I didn't write this myself, I got the code from 4guysfromrolla.com and
they missed it out too

"Scott M." wrote:
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

"ArtySin" <Ar*****@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
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

*****************************************

"Scott M." wrote:
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.

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:67*************@mid.individual.net...
ArtySin wrote:
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



Jun 27 '08 #8
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!
"ArtySin" <Ar*****@discussions.microsoft.comwrote in message
news:14**********************************@microsof t.com...
No, I didn't write this myself, I got the code from 4guysfromrolla.com and
they missed it out too

"Scott M." wrote:
>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

"ArtySin" <Ar*****@discussions.microsoft.comwrote in message
news:9C**********************************@microso ft.com...
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

*****************************************

"Scott M." wrote:

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.

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:67*************@mid.individual.net...
ArtySin wrote:
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




Jun 27 '08 #9
Andrew Morton wrote:
>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?
Scott M. wrote:
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:

ArtySin wrote:
lblRawOutput.Text = contents
<snip>
<asp:label runat="server" id="lblRawOutput" />
Of course, the OP should check for File.Exists before trying to read it.

Andrew
Jun 27 '08 #10
But again, server-side code doesn't render like that. An ASP .NET label
renders as an HTML <spanelement.
"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:67*************@mid.individual.net...
>Andrew Morton wrote:
>>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?

Scott M. wrote:
>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:

ArtySin wrote:
> lblRawOutput.Text = contents
<snip>
> <asp:label runat="server" id="lblRawOutput" />

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

Andrew

Jun 27 '08 #11

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

Similar topics

9
by: ShadowOfTheBeast | last post by:
Hi, I have got a major headache understanding streamReader and streamWriter relationship. I know how to use the streamreader and streamwriter independently. but how do you write out using the...
3
by: Mika M | last post by:
Hello! I'm reading text file line by line using the StreamReader like code below shows. Reading is working fine, but if readed line contains Scandinavian letters like äÄöÖ then those letters are...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
2
by: James Wong | last post by:
Dear all, I'm using StreamReader to read a text file containing BIG-5 data and found that no matter which encoding method in StreamReader's construction parameter, the BIG-5 contents become...
11
by: LucaJonny | last post by:
Hi, I've got a problem using StreamReader in VB.NET. I try to read a txt file that contains extended characters and theese are removed from the line that is being read. I've read a lot of...
1
by: JM | last post by:
Hi, I have been trying to read a file, using "StreamReader" to pass the info to an string that is: ....dim stream as new FileStream(file,...) dim sr as new StreamReader(stream) dim data as...
4
by: George | last post by:
Hi, I am puzzled by the following and seeking some assistance to help me understand what happened. I have very limited encoding knowledge. Our SAP system writes out a text file which includes...
4
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another...
1
by: Sladan | last post by:
Im trying to read a xml-file with a StreamReader. For the moment I'm using the following code. streamReader = new StreamReader(stream, System.Text.Encoding.Default); string feedData =...
0
by: rajana | last post by:
Dear All, We have Ansi file with german characters (Ä / Ø) , We are using Streamreader to read the contents of the file. But Readline() not able to read the German characters. We tried all...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
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 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.