0

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

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    Depends a bit on where in the asp.net lifecycle of events you call these Response.Writes. I'm not sure what happens if you write to the stream before the http headers are send, nor what will happen after Render is complete. – rene Mar 07 '17 at 22:43
  • 1
    Wouldn't it be easier to do a Trace.Write and then visit /trace.axd to see those messages: https://msdn.microsoft.com/en-us/library/bb386420.aspx – rene Mar 07 '17 at 22:44
  • I don't know if it had anything to do with it, but adding trace="true" caused the page to no longer even load. It just hourglasses forever. And even after changing it back to "false"...??? A rebuild fixed it... – B. Clay Shannon-B. Crow Raven Mar 07 '17 at 23:01
  • I see a ton of information on the Trace.axd page, but none of those things I wrote out are there. – B. Clay Shannon-B. Crow Raven Mar 07 '17 at 23:10
  • Where is this code called? Is it in a page or in an event in a codebehind? If in a codebehind, which event. – rene Mar 07 '17 at 23:51
  • All the code for the page is in one file: VB, html, javascript, all jumbled and mashed together like a Frankensteinian mosh pit. It runs inside a conditional block "If Request.Form.Item("Action") = "Save" Then..." – B. Clay Shannon-B. Crow Raven Mar 08 '17 at 00:00
  • 1
    Let me walk you through the steps to get tracing enabled:add in the \web,config in the section `` this element: ``. Got any of your pages, I'll use `/about.aspx` as an example. Either inline or in a codebehind type: `HttpContext.ApplicationInstance.Context.Trace.Write("testing","fubar")`. Can you verify that you don't have an `` or if you do that it doesn't clear the ``. Start your app and hit /about.aspx. then navigate to /trace.axd and find the /about.aspx and click view details at the right. – rene Mar 08 '17 at 08:32
  • 1
    Under the heading `Trace Information Category` you should find the text *testing* and *fubar*. – rene Mar 08 '17 at 08:34
  • 1
    It almost looks like that code block is inside another `With` statement. I'm a bit baffled by the fact that you got that `non-shared member requires an object reference.` error because I was afraid you would run into that one which was why I offered that particular code.. Maybe one more try to make it Trace: `System.Web.HttpContext.Current.Trace.Write("foo", "bar")`. And please edit in your web.config the trace line to read ``. Set the requestLimit to a reasonable high value. – rene Mar 08 '17 at 16:34
  • Interesting - that works, in that there is no err page, but Trace.axd shows me no Trace write data that I can see, scanning through it all. Now custmaint_entry.aspx does display in the list of hit pages (twice, at the bottom), but following their links shows no Trace.Write() calls. Maybe it's not expected to? But there is no change in the View Source display, either... – B. Clay Shannon-B. Crow Raven Mar 08 '17 at 16:49
  • The "HttpContext.Current" instead of "HttpContext.ApplicationInstance.Context" seems to be what was needed to prevent the err page. The "System.Web" prepended portion is greyed out and so apparently superfluous. – B. Clay Shannon-B. Crow Raven Mar 08 '17 at 16:51
  • 1
    Yeah., I tried it this morning in an MVC app because that was what I had running. I didn't think about possible issues with .net 2.0 or plain asp.net. As you now got output, did you put the Trace.Write() near the start of your page, at a line that is most likely to be hit? Because then we created ourselves an opportunity to debug this issue. – rene Mar 08 '17 at 17:19
  • I know the code where I put it is being hit, because the database writes are being made. Thanks, but I've got it working now, so the need to debug has gone to the back burner as I go on to the SSRS report portion of this task. Combine your comments into an answer, and I will gladly mark it as such. – B. Clay Shannon-B. Crow Raven Mar 08 '17 at 17:23
  • Ok, give me a couple of minutes – rene Mar 08 '17 at 17:25

1 Answers1

2

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.

rene
  • 41,474
  • 78
  • 114
  • 152