C# setting the .Text value of a label from a static method

Howdy folks, I’m fairly new at programming in C# and I’m having a problem with something that should be really simple.

I’ve got a static method that sends emails after doing some page processing, but it’s having trouble when I publish it out of my localhost environment. Since I can’t really debug a compiled version I put a label on the page then I want this method to use a try/catch

catch(System.Exception ex)
	lbl_Errors.Text = "crap";
	return false;

and I get this error:

c:\inetpub\wwwroot\IS_Apps\PMLic\WebApps\PMLic\Reports\delinquentRosters.aspx.cs(257): ‘IS_Apps.PMLic.WebApps.PMLic.Reports.delinquentRosters.lbl_Errors’ denotes a ‘field’ where a ‘class’ was expected

I know it has to do with labels being considered “non-static” and the static method can’t address them. If I do this:

private void crap()
	lbl_Errors.Text = "crap";

it works, but to call it I need an object reference. I’m not creating any objects for this process, I just want the label on the page to take a new value so I can read these exceptions on the server. And yes, I’m am getting exceptions just fine, I just need to pass the text to this label, or anything I can read on the page really.

Any advice on passing data from a static method to the screen? This is a web application, so MessageBox doesn’t work either.

To see exceptions set CustomErrors=“off” in web.config and do not catch them in code. Just let them pass through. ASP.NET will eventually catch any uncaught exception and will throw up the familar yellow exception page. If custom errors have been switched off you’ll get the exception details including stacktrace. (once in production you switch customerrors “on” and can then avoid your site/pages spill their guts in case of errors).

You may also want to set debug=“true” in the compilation element. That’ll give you line numbers in the stacktrace.

If you want to print diagnostics output during page processing then add a Trace=“true” in the @Page directive. You can then use Trace.Write to write to the trace output, complete with timings, order in lifecycle etc. Use Page.Trace.Write from controls or HttpContext.Current.Trace.Write from static methods, handlers etc. Once you switch Trace=“false” (or remove the Trace attribute) the diagnostics output will dissapear. With Trace=“true” the trace will even show exceptions (with stacktrace) in red.

I usually have the email problem wen deploying to a server, so its more than likely smtp settings or something not working.

As for the static method to write to the label. Static methods are not accessible to the rest of the page or vise versa.

usually 2 do something like that you would have a static method return a string:

public static string crap()
return “crap”;

then on the page: lbl_Errors.Text = className.crap();

Hope this helps

If you need access to the page (if there is any) from a static method you can always try something like:

Page page = HttpContext.Current.Handler as Page;

That’ll give you access to the page, if the current handler is indeed a page.

Better yet, make the static methods take a reference to the objects you want to play with. Much cleaner.