Replacing console.writeline in asp.net page

hi,

i need to replace the console.writeline calls in the following code with something to which will output to the page so that I can check the code is working. I;ve tried using trace.write but I get an error stating that it doesnt work in this context.

can anyone help? code is below.

thanks,

lukemack

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using eBay.Service.Core.Sdk;
using eBay.Service.Call;
using eBay.Service.Core.Soap;

public partial class testing : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Execute();
    }
    static void Execute()
    {
        ApiContext context = new ApiContext();
        // Credentials for the call
        context.ApiCredential.ApiAccount.Developer = "removed for post";
        context.ApiCredential.ApiAccount.Application = "removed";
        context.ApiCredential.ApiAccount.Certificate = "removed";
        context.ApiCredential.eBayToken = "removed";

        // Set the URL
        context.SoapApiServerUrl = "https://api.sandbox.ebay.com/wsapi";
        try
        {
            // Create the call object
            GetUserCall call = new GetUserCall(context);
            String requestedUser = "yoursandboxuser";
            UserType user = call.GetUser(requestedUser);
            // Print the results to standard out
            string test = "eBay Time: " + call.AbstractResponse.Timestamp.ToString();
            Console.WriteLine("User retrieved: " + user.UserID);
            Console.WriteLine("Feedback score: " + user.FeedbackScore);
            Console.WriteLine("Feedback star: " + user.FeedbackRatingStar);
            Console.WriteLine("Seller level: " + user.SellerInfo.SellerLevel);
            Console.WriteLine("User's registered site: " + user.Site);
        }

        catch (ApiException e)
        {
            Console.WriteLine("Problem getting user. Errors listed below.");
            foreach (ErrorType error in e.Errors)
            {
                Console.WriteLine("Error: " + error.ShortMessage);
                Console.WriteLine("Message: " + error.LongMessage);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Unexplained error in making the API call" + e.StackTrace);
        }
    }

}

For testing if the code is working, you really should not need any extra output. Just use breakpoints and watches as appropriate.

Trace.Write works fine. But it requires a page and should not be executed from a static method.

You must enable trace for the page to see the output from the trace. Or you could a

<trace enabled=“true” writeToDiagnosticsTrace=“true”/>

inside <system.web> in web.config to have the output show up in the debug output window of VS2005.

I have trace enabled but what I mean is if I place it within the execute function, I get:

Compiler Error Message: CS0120: An object reference is required for the nonstatic field, method, or property ‘System.Web.UI.Page.Trace.get’

Or If I try to trace in the page_load for ‘user’ I get:

Compiler Error Message: CS0103: The name ‘user’ does not exist in the current context

I think the issue is one of me non understanding how the different contexts work.

any advice would be greatly appreciated.

i have resolved this by removing the execute function and putting everything in the page_load. this allowed the trace.write function to work.

i’d still appreciate it if someone could offer me a brief explanation of why this is the case, though? I know it has something to do with the page class and contexts but am unsure exactly what (am still getting to grips with asp.net and OOP)

many thanks for the replies,

lukemack.

If your using Trace.Write within a class, you need to use the current instance of the HttpContext:


HttpContext.Current.Trace.Write("something");

Or just use Response.Write(“Message”);

response.write is also not available within the execute function - again due to contexts i think. I cant test the httpcontext method now as I have changed the code.

thanks again for the replies.

Response.Write is NOT A debugging tool. Ignoring the lunacy of using it in a .NET page, the other issue is that you are now writing code you must change to bring things back to working for debugging purposes. Potentially creating more bugs.

Trace is the “proper” way to do it if you need something permanently. If you don’t need something permananent, just set a breakpoint and walk through the code as necessary.

Any reason why your Execute function is static?
I would make it a “full” member of the class (not static).

i have resolved this by removing the execute function and putting everything in the page_load

Ok… You have already done it. Removing the static keyword from the function signature would achive the same.

You can now access all members of the Page class, Response, Trace, etc.

cheers,
rui

^^^It was static to work from the command line. (public static void Main(string args))