Solved

Debugging a C# Web program

Posted on 2016-07-27
3
55 Views
1 Endorsement
Last Modified: 2016-08-01
I have to modify a program that was written for the web.  When I run the code using Visual Studio there isn't a problem.  When I 'Publish' the code and run it from my Chrome browser I start to have problems.

When I have written ASP classic programs I am able to set a bunch of response.write to see what variables are.  In C# it doesn't appear I can do the same thing.  At least I can't figure out how to do it.

I have this line of code but I don't know where to put it to view it.  I am assuming it will need to be on the same page that is published.  For example, Cm.cpt is my connection string.  This is set in a page that is not shown to the user.  When the Login.aspx page is shown, how do I show the connection string?

 HttpResponse Response;

            Response = System.Web.HttpContext.Current.Response;
            Response.Write("Cm.cpt= " + Cm.cpt + "<br>");

Open in new window


How do I display it on my page?
1
Comment
Question by:huerita37
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 42

Expert Comment

by:pcelba
ID: 41733258
It should be possible to create the new web app in IIS, set the root folder of published application as the Working folder and then attach the VS debugger into IIS worker process. Then you may use breakpoints and look at variable values or evaluate expressions in Immediate window etc.
0
 

Author Comment

by:huerita37
ID: 41734750
pcelba - I may  not be understanding you completely because I am able to debug and step through the code while I run the program through VS.  My program runs fine when I run it through VS.  It's when it is live that I am having problems.  I want to see what certain variables are at particular times while the code is LIVE.

Can you confirm that your suggestion is for the LIVE program?
0
 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 41734945
No, I cannot confirm it but how different is the application running under IIS on your local PC from the production one?

OK, if you really need values from the production then you may use Response.Write and you don't need to declare it as it is done in automatically generated metafiles.

Let suppose you are using C# language.

Simply create any ASPX page and to the code behind (.aspx.cs file) place the
Response.Write("Your text string");
into e.g. Page_Load method and that's it.

We are using this code to display Session keys:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class My_ShowSession : PageBase // System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        long total = 0;
        foreach (string key in Session.Keys)
        {
            object value = Session[key];
            long objectSize = ObjectSize(value);
            total += objectSize;
            Response.Write("Size:" + objectSize + " | " + key + "<BR/>");
            WriteDetails(value);
            Dictionary<string, object> s = Session[key] as Dictionary<string, object>;
            if (s != null)
            {
                if (s.ContainsKey("__timestamp"))
                {
                    Response.Write("&nbsp; &nbsp; TIMESTAMP is:" + ((DateTime)s["__timestamp"]).ToString()+"<br/>");
                }
            }
        }
        Response.Write("Total size:" + total);
    }

    private void WriteDetails(object value)
    {
       // format and write details here
    }


    private long ObjectSize(object obj)
    {
        try
        {
            System.IO.MemoryStream m = new System.IO.MemoryStream();
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            b.Serialize(m, obj);
            return m.Length;
        }
        catch { }
        return 0;
    }

}

Open in new window

And the ASPX code is very simple:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SessionInfo.aspx.cs" Inherits="My_ShowSession" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Session Info</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window

0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question