Solved

Debugging a C# Web program

Posted on 2016-07-27
3
48 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
  • 2
3 Comments
 
LVL 41

Expert Comment

by:pcelba
Comment Utility
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
Comment Utility
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 41

Accepted Solution

by:
pcelba earned 500 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now