htmlresponse.writeview-source

Why are all but one of my html debug msgs/comments (Response.Write) not being written to the "View Source"?


I have this code that I know is getting hit, because the insert is being accomplished:

Response.Write("<!-- Made it just before INSERT INTO CustomerCategoryLog -->")
Dim query As String = String.Empty
query &= "INSERT INTO CustomerCategoryLog (MemberNo, Unit, Custno, "
query &= "Category, Subcategory, BeginDate, "
query &= "EndDate, ChangedBy, ChangedOn) "
query &= "VALUES (@MemberNo, @Unit, @Custno, @Category, @Subcategory, @BeginDate, @EndDate, @ChangedBy, @ChangedOn)"

Using conn As New SqlConnection("SERVER=PROSQL05;DATABASE=cpsdata;UID=sa;PWD=sqlsql")
    Using comm As New SqlCommand()
        Response.Write("<!-- Made it into the using comm As New SqlCommand() block -->")
        With comm
            .Connection = conn
            .CommandType = CommandType.Text
            .CommandText = query
            .Parameters.Add("@MemberNo", SqlDbType.NVarChar).Value = MemberNo
            .Parameters.Add("@Unit", SqlDbType.NVarChar).Value = Unit
            .Parameters.Add("@Custno", SqlDbType.NVarChar).Value = CustNo
            .Parameters.Add("@Category", SqlDbType.NVarChar).Value = Category
            .Parameters.Add("@Subcategory", SqlDbType.NVarChar).Value = Subcategory
            .Parameters.Add("@BeginDate", SqlDbType.DateTime).Value = Date.Now()
            .Parameters.Add("@EndDate", SqlDbType.DateTime).Value = Date.Now().AddDays(365)
            .Parameters.Add("@ChangedBy", SqlDbType.NVarChar).Value = Environment.UserName
            .Parameters.Add("@ChangedOn", SqlDbType.DateTime).Value = Date.Now()
        End With
        Try
            Response.Write("<!-- Made it into the try block -->")
            conn.Open()
            comm.ExecuteNonQuery()
        Catch '(ex as SqlException)
            Response.Write("<!-- Made it into the catch block -->")
        End Try
    End Using 'comm
End Using 'conn

Yet none of the Response.Write()s are get rendered to the page, as "View Source" shows.

Why aren't they? In a previous place, a comment/debug msg is getting written, using the same syntax:

Response.Write("<!-- IsNewBusiness after NOT adoRS.EOF check = " & CStr(IsNewBusiness) & " -->")

Why do subsequent calls to Response.Write() do nothing?

UPDATE

There is a "system.diagnostics" section in Web.Config, with this content:

<system.diagnostics>
    <sources>
      <!-- This section defines the logging configuration for My.Application.Log -->
      <source name="DefaultSource" switchName="DefaultSwitch">
        <listeners>
          <add name="FileLog" />
          <!-- Uncomment the below section to write to the Application Event Log -->
          <add name="EventLog" />
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
      <add name="FileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter" />
      <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
      <add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="eServices" />
    </sharedListeners>
  </system.diagnostics>

As you can see, there is a "sharedListeners" subsection, but as far as I can tell, it's not clearing them...

UPDATE 2

This is what I get after prepending "HttpContext.ApplicationInstance.Context." to "Trace.Write(":

Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30469: Reference to a non-shared member requires an object reference.

Source Error:



Line 184:            ' Put the Category Table insert here, too...
Line 185:            ' Beginning of SQL Insert code added 3/6/2017 - if not reached, move it above the "END IF"
Line 186:            HttpContext.ApplicationInstance.Context.Trace.Write("<!-- Made it just before INSERT INTO CustomerCategoryLog -->")
Line 187:            Dim query As String = String.Empty
Line 188:            query &= "INSERT INTO CustomerCategoryLog (MemberNo, Unit, Custno, "


Source File: C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 186 

UDPATE 3

A (very, to me) strange thing is that when I got trace.axd, it does not list the file in question - the one into which I have inserted the calls to Trace.axd:

enter image description here

but I am on that page (though it doesn't show in the URL, which remainshttp://localhost/EMS/customerreportingnet), when I append the "/trace.axd" to the URL and hit the Enter key.

Why would the file not appear in the list?

If I "View Source" from that page, I do see some of its output, such as ""

So custmaint_entry.aspx is being accessed - which is also seen by the URL in the "View Source" pane:

enter image description here


Solution

  • If Response.Write calls get written in such a way that they end up in the html payload as presented by your browser depends on where in the ASP.NET Lifecycle you place those calls.

    By the looks of it you're trying to debug/trace code flow in a complex webpage. I would personally rely on the Tracing facilities offered by the .Net Framework and ASP.NET.

    In the root of your webapplicaton edit the web.config to include/update the system.web tag with the trace tag.

    <config>
       <!-- lots of stuff -->
        <system.web>
            <trace enabled="true" 
                   pageOutput="false" 
                   requestLimit="1337" 
                   localOnly="false"/>
        </system.web>
        <!-- lots of stuff -->
    </config>
    

    For completeness check if you have a system.diagnostics tag and verify that in the <sharedListeners> tag there isn't a <clear/> element.

    On one of your pages (let's say /about.aspx) add in the code (either inline or in the code behind):

    System.Web.HttpContext.Current.Trace.Write("foo", "bar")
    

    Now save and run your webapp and navigate to /about.aspx and make sure you exercise such that the above Trace.Write line is being executed.

    Visit /trace.axd. You'll get an overview of the pages that have been served. Find your /about.aspx line and click on the View Details link at the right. That brings you to the detail page which should contain your trace messages.

    On the start page of /trace.axd you can also clear the trace by clicking the link at the top left.

    Using Trace messages above writing to the Response stream has the benefit that those Trace calls can be left in the code for production and then enabled at will. Sending too much data over the wire to browsers might slowdown the response times of your application.