Solved

access this function in multiple files

Posted on 2006-11-06
5
177 Views
Last Modified: 2010-04-16
In my c# web app, I have a bunch of aspx pages that I want to add error catching, try catch and finally code and anytime the catch exception is invoked I want to call a common function(send an email) I have in my helper.cs file - code below.
The problem is I dont know how to access this function that is in my class, the helper file from my aspx.cs file. I dont want to copy this code accross all my aspx.cs file.

Assistance would be appreciated.

public void SendEmailOpen( string strSubject, string strContent, string strTo, string strCc )
{
      //send email to the admins - get admin list from web.config
      MailMessage mail = new MailMessage();
      
      mail.From = ConfigurationSettings.AppSettings["email_from"];
      mail.Subject = strSubject;
      mail.Body = strContent;
      mail.BodyFormat = MailFormat.Html;
      mail.Priority = MailPriority.High;

      mail.To = strTo;
      mail.Cc = strCc;

      SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["email_server"];
      try
      {
            SmtpMail.Send(mail);
      }
      catch (Exception ex)
      {
            while( ex.InnerException != null )
            {                              
                  ex = ex.InnerException;
            }
      }
}
0
Comment
Question by:solraccheffy
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 50 total points
ID: 17886952
Not sure what caused the problem...

I'd make the method static so you don't need to create an instance of the helper class in each page:
public static void SendEmailOpen

If your helper class is called like 'Helper', you invokethe method:

Helper.SendEmailOpen(....)
0
 

Author Comment

by:solraccheffy
ID: 17889083
Thanks for the reply, I changed to the static method as you described, but in my .cs file when I specify the line as you described to invoke....the autotype does not kick in....I typed in Helper. and nothing is coming up, is there something else I need to do to reference/invoke this method?

Thanks.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 17892470
If your class is in a different project you need to import it ("using" keyword) and may be to add a reference to the project from project references.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

13 Experts available now in Live!

Get 1:1 Help Now