Web form event referencing a classhttp://forums.asp.net/t/1796055.aspx/1?Web+form+event+referencing+a+classWed, 25 Apr 2012 07:27:40 -040017960554947101http://forums.asp.net/p/1796055/4947101.aspx/1?Web+form+event+referencing+a+classWeb form event referencing a class <pre class="prettyprint">I am having an issue referencing my namespance for sending an email in a contact codebehind event call. I can't seem to get EmailHelper to work, keep getting a context error</pre> <pre class="prettyprint">&nbsp;</pre> <pre class="prettyprint"> protected void Button1_Click(object sender, EventArgs e) { EmailHelper comm = new EmailHelper(); comm.SendMailMessage(txtEmail.Text, txtNotes); }</pre> <pre class="prettyprint">&nbsp;</pre> <pre class="prettyprint">The namespace Comm is supposed to handle all things communication related where the EmailHelper method SendMailMessage resides</pre> <pre class="prettyprint">&nbsp;</pre> <pre class="prettyprint">using System.Net.Mail; namespace Comm { public class EmailHelper { public static void SendMailMessage(string email, string message) { MailMessage emessage = new MailMessage(); emessage.From = new MailAddress(email); emessage.To.Add(new MailAddress("scott@hopefestaz.com")); emessage.Body = message; emessage.Priority = MailPriority.High; SmtpClient smtp = new SmtpClient(); smtp.Send(emessage); } } } </pre> <pre class="prettyprint"><br /><br /></pre> <pre class="prettyprint">&nbsp;</pre> <pre class="prettyprint">&nbsp;</pre> 2012-04-23T18:53:37-04:004947118http://forums.asp.net/p/1796055/4947118.aspx/1?Re+Web+form+event+referencing+a+classRe: Web form event referencing a class <p>if it's static, you don't need a new instance. You should be able to access it directly.</p> <p>Try removing the static keyword.</p> 2012-04-23T19:03:40-04:004947128http://forums.asp.net/p/1796055/4947128.aspx/1?Re+Web+form+event+referencing+a+classRe: Web form event referencing a class <p>Still getting a could not find issue, the &quot;am I using an assembly or reference?&quot;&nbsp; hint</p> 2012-04-23T19:14:44-04:004947412http://forums.asp.net/p/1796055/4947412.aspx/1?Re+Web+form+event+referencing+a+classRe: Web form event referencing a class <p>I have check below script code, it is working,</p> <pre class="prettyprint">protected void Button1_Click(object sender, EventArgs e) { EmailHelper.SendMailMessage(txtEmail.Text, txtNotes); }</pre> <p><br> <br> </p> 2012-04-24T00:40:26-04:004950084http://forums.asp.net/p/1796055/4950084.aspx/1?Re+Web+form+event+referencing+a+classRe: Web form event referencing a class <p>Hi fredarator,</p> <p>&nbsp;It seems like that the class <span class="typ">EmailHelper</span><span class="pln"> </span>is in the namespace&nbsp;<strong>Comm</strong>. &nbsp;In order to use the method <span class="typ"> SendMailMessage in the code behind</span>, you can add the namespace and&nbsp;modify the code as follows:</p> <pre class="prettyprint">protected void Button1_Click1(object sender, EventArgs e) { Comm.EmailHelper comm = new Comm.EmailHelper(); comm.SendMailMessage(txtEmail.Text, txtNotes); }</pre> <p>Or add the namespace in the code behind&nbsp;as follows:</p> <pre class="prettyprint">using Comm;</pre> <p>In addition, you need to delete the <strong>static </strong>for your method.</p> <p>You can also follow aamirha's advice to call the method directly.</p> <p>Best wishes,</p> 2012-04-25T07:27:40-04:00