StreamReader and text file problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QXJ0eVNpbg==?=

    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="Syst em.IO" %>
    <script language="vb" runat="server">
    sub Page_Load(sende r as Object, e as EventArgs)

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

    Dim objStreamReader as StreamReader
    objStreamReader = File.OpenText(F ILENAME)

    Dim contents as String = objStreamReader .ReadToEnd()


    lblRawOutput.Te xt = contents

    objStreamReader .Close()
    end sub
    </script>

    <b>Meeting Minutes</b><br />
    <asp:label runat="server" id="lblRawOutpu t" />


  • Scott M.

    #2
    Re: StreamReader and text file problem

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

    Dim FILENAME as String = Server.MapPath( "~minutes.t xt")


    "ArtySin" <ArtySin@discus sions.microsoft .comwrote in message
    news:159B119F-0B88-4F66-8D79-E13F3086A25A@mi crosoft.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="Syst em.IO" %>
    <script language="vb" runat="server">
    sub Page_Load(sende r as Object, e as EventArgs)
    >
    Dim FILENAME as String = Server.MapPath( "minutes.tx t")
    >
    Dim objStreamReader as StreamReader
    objStreamReader = File.OpenText(F ILENAME)
    >
    Dim contents as String = objStreamReader .ReadToEnd()
    >
    >
    lblRawOutput.Te xt = contents
    >
    objStreamReader .Close()
    end sub
    </script>
    >
    <b>Meeting Minutes</b><br />
    <asp:label runat="server" id="lblRawOutpu t" />
    >
    >

    Comment

    • =?Utf-8?B?QXJ0eVNpbg==?=

      #3
      Re: StreamReader and text file problem

      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.t xt")
      >
      >
      "ArtySin" <ArtySin@discus sions.microsoft .comwrote in message
      news:159B119F-0B88-4F66-8D79-E13F3086A25A@mi crosoft.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="Syst em.IO" %>
      <script language="vb" runat="server">
      sub Page_Load(sende r as Object, e as EventArgs)

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

      Dim objStreamReader as StreamReader
      objStreamReader = File.OpenText(F ILENAME)

      Dim contents as String = objStreamReader .ReadToEnd()


      lblRawOutput.Te xt = contents

      objStreamReader .Close()
      end sub
      </script>

      <b>Meeting Minutes</b><br />
      <asp:label runat="server" id="lblRawOutpu t" />
      >
      >
      >

      Comment

      • Andrew Morton

        #4
        Re: StreamReader and text file problem

        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


        Comment

        • Scott M.

          #5
          Re: StreamReader and text file problem

          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" <akm@in-press.co.uk.inv alidwrote in message
          news:675jeuF2n5 6e5U1@mid.indiv idual.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
          >

          Comment

          • =?Utf-8?B?QXJ0eVNpbg==?=

            #6
            Re: StreamReader and text file problem

            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" <akm@in-press.co.uk.inv alidwrote in message
            news:675jeuF2n5 6e5U1@mid.indiv idual.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
            >
            >
            >

            Comment

            • Scott M.

              #7
              Re: StreamReader and text file problem

              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" <ArtySin@discus sions.microsoft .comwrote in message
              news:9C51065F-C0EF-434B-BCEE-03E7ECE91F00@mi crosoft.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" <akm@in-press.co.uk.inv alidwrote in message
              >news:675jeuF2n 56e5U1@mid.indi vidual.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
              >
              >>
              >>
              >>

              Comment

              • =?Utf-8?B?QXJ0eVNpbg==?=

                #8
                Re: StreamReader and text file problem

                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" <ArtySin@discus sions.microsoft .comwrote in message
                news:9C51065F-C0EF-434B-BCEE-03E7ECE91F00@mi crosoft.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" <akm@in-press.co.uk.inv alidwrote in message
                news:675jeuF2n5 6e5U1@mid.indiv idual.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

                >
                >
                >
                >
                >
                >

                Comment

                • Scott M.

                  #9
                  Re: StreamReader and text file problem

                  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" <ArtySin@discus sions.microsoft .comwrote in message
                  news:14CFA41D-BFDB-4967-AF7A-75F252F2AA76@mi crosoft.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" <ArtySin@discus sions.microsoft .comwrote in message
                  >news:9C51065 F-C0EF-434B-BCEE-03E7ECE91F00@mi crosoft.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" <akm@in-press.co.uk.inv alidwrote in message
                  >news:675jeuF2n 56e5U1@mid.indi vidual.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
                  >
                  >>
                  >>
                  >>
                  >>
                  >>
                  >>

                  Comment

                  • Andrew Morton

                    #10
                    Re: StreamReader and text file problem

                    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.Te xt = contents
                    <snip>
                    <asp:label runat="server" id="lblRawOutpu t" />
                    Of course, the OP should check for File.Exists before trying to read it.

                    Andrew


                    Comment

                    • Scott M.

                      #11
                      Re: StreamReader and text file problem

                      But again, server-side code doesn't render like that. An ASP .NET label
                      renders as an HTML <spanelement.


                      "Andrew Morton" <akm@in-press.co.uk.inv alidwrote in message
                      news:678862F2ne 5foU1@mid.indiv idual.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.Te xt = contents
                      <snip>
                      > <asp:label runat="server" id="lblRawOutpu t" />
                      >
                      Of course, the OP should check for File.Exists before trying to read it.
                      >
                      Andrew
                      >

                      Comment

                      Working...