0

I am building features for a .NET framework 4.5 codebase but I'm having issues debugging.

For context, up until this point I've built JavaScript/Node.js projects and seeing what data are inside of my variables was as easy as including console.log(JSON.stringify(some_object)) into any part in my codebase. This worked like a charm because it would show me my variables data in real-time via the browser dev tools.

I'm having trouble debugging on this .NET web site (not web application) project. It's using the web forms approach so I have a bunch of .aspx and .aspx.cs files. A big difference from the previous codebases I've worked on to the one I'm working on now is that before, I had to compile my JS/Node.js projects beforehand, whereas this web site project compiles each page on the fly on each request. Interesting "build" process but as a result I haven't found much insight on the debugging process. I have access to both dev and live.

Question How can I access my variables from Visual Studio? The functions in my code-behind files always trigger when I go to their respective .aspx page. I want to see what my SQL/API calls are receiving and saving into my variables.

I've already tried to show my data in my dev tools but I am blocked from doing so because I enter quirks mode so I rather stay away from showing my variables content in the browser's console.

jfar41
  • 59
  • 8
  • I've tried this and it is not working. I have debug=True in my webconfig. https://stackoverflow.com/questions/14713782/how-to-show-console-writeline-output-in-my-browser-console-or-output-window – jfar41 Dec 15 '22 at 16:50

1 Answers1

0

Gee, talking about debugging in Visual Studio, and wondering what your options are? One of the premiere developer tools available?

Might as well walk into a big garden supply depot, and wonder if they will have lawnmowers and lawn care products!!

So, first up, and there are MORE choices then flavors of ice cream here, but I tend to send my output(s) to the immediate window. That reuslts in when I am building windows apps, console apps, web apps, or whatever to send out my debug.print statements to the immediate window. (I tend to do this, since I am flpping between desktop software development, console development, and even building com .net objects for ms-office).

So, this:

tools->options->debugging

Then this:

enter image description here

The other reason, is that immediate window opens when you hit f5 to run/start your code.

As for having to use debug.print? Well, often you don't have to.

I assume you working a "copy" and have VS setup to run this project on your dev box.

So, say I have some simple markup in a page, simple grid.

<asp:Button ID="cmdLoadData" runat="server" Text="Load Grid Data"  
    CssClass="btn" OnClick="cmdLoadData_Click"/>
<br />

<asp:GridView ID="GridView1" runat="server" CssClass="table table-hover"
    width="40%" DataKeyNames="ID" AutoGenerateColumns="false" >
    <Columns>
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="HotelName" HeaderText="Hotel" />
        <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Description") %>' ></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And code behind is this for the button:

protected void cmdLoadData_Click(object sender, EventArgs e)
{
    Debug.Print("Load grid data");
    LoadData();          
}

void LoadData()
{
    string sCon = ConfigurationManager.ConnectionStrings["TEST4"].ConnectionString;
    Debug.Print(sCon);

    using (SqlConnection conn = new SqlConnection(sCon))
    {
        string strSQL = @"SELECT * FROM tblHotelsA ORDER BY HotelName";
        using (SqlCommand cmd = new SqlCommand(strSQL, conn))
        {
            conn.Open();
            DataTable rstData = new DataTable();
            rstData.Load(cmd.ExecuteReader());
            GridView1.DataSource = rstData;
            GridView1.DataBind();
        }
    }

However, lets assume I not YET put a debug.print in for the number of rows sql returns.

Well, really, don't need debug.print, I can just put in a break-point, say on the first line of the button click, and now single step code.

So, say I have this code, but I ALSO clicked on the left side - you see the red break-point.

So, this code:

But, in the editor, I have a break point set, so it looks like this:

enter image description here

So, when I hit f5 to run/debug, my code will run to the breakpoint above,

I want to see the data columns, and also want to know/see the number of rows returned by the query. And I do NOT even need any debug.prints

So, I single step. And you can just hover your cursor over any object - VS will like "magic" peel open that object like a onion!

So, I single step (F8), It looks like this:

enter image description here

so, in above, I did see/check the columns, and then I took a quick look at the rows property, and got my row count.

So the debug window (now the immedage window looks like this:

enter image description here

And there are bunch more windows that you can have open.

So, say the Immedate window - (where debug.print goes).

So, this:

enter image description here

Now, I barely scratched the surface.

but, as a general idea, if you have a button on a web form, you run to that page, and set a break-point in the code behind. At that point, you can single step as I did above (f8).

But, in place of a boatload of debug.prints, I just hover the cursor over the object (in this example a data table), and I was able to see/view the columns, and then I moved over to the row object to get a row count.

of course on the browser side, I get this:

enter image description here

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51