0

I have this simple form I am testing built in VS2012 and vb.net. The code sends the email twice and I did put the breakpoint and I don't know why it loops twice.

Code:

Protected Sub btnSubmit_OnClick(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim formBody As String
    formBody = "<b>Donation Amount: <b/>" + txtAmount.Text.Trim()
    Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
    mailMessage.From = New System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings("fromEmailAddress"))
    mailMessage.To.Add(New System.Net.Mail.MailAddress("webmaster@example.com"))
    mailMessage.IsBodyHtml = True
    mailMessage.Subject = "Donation Form"
    mailMessage.Body = formBody

    Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
    smtpClient.Send(mailMessage)

    Response.Redirect("MemDonation_Confirmation.aspx", False)
End Sub

This is the form:

<asp:Label runat="server" ID="lblAmount" Text="Donation Amount: "></Label>    
<asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
<asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_OnClick" Text="Submit" style="height: 26px" /> 

Call Stack looks like this and I don't know what to make out of:

Step into: Stepping over non-user code 'MemDonation.txtAmount.get'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.MailMessage'
Step into: Stepping over non-user  code 'System.Configuration.ConfigurationManager.AppSettings.get'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.To.get'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.IsBodyHtml.set'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.Subject.set'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.Body.set'
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.SmtpClient'
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.Send'
Step into: Stepping over non-user code 'System.Web.UI.Page.Response.get'
Step into: Stepping over non-user code 'MemDonation.txtAmount.get'
 Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.MailMessage'
Step into: Stepping over non-user code 'System.Configuration.ConfigurationManager.AppSettings.get'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.To.get'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.IsBodyHtml.set'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.Subject.set'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.Body.set'
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.SmtpClient'
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.Send'
Step into: Stepping over non-user code 'System.Web.UI.Page.Response.get'
Step into: Stepping over non-user code 'System.Web.UI.WebControls.Button.OnClick'
Step into: Stepping over non-user code 'System.Web.UI.WebControls.Button.RaisePostBackEvent'
Step into: Stepping over non-user code 'System.Web.UI.Page.ProcessRequestMain'
 Step into: Stepping over non-user code 'ASP.memdonation_aspx.GetTypeHashCode'
Step into: Stepping over non-user code 'System.Web.UI.Page.SaveAllState'
Step into: Stepping over non-user code 'System.Web.UI.Page.ProcessRequestMain'
Step into: Stepping over non-user code 'System.Web.UI.Page.ProcessRequest'
Step into: Stepping over non-user code 'System.Web.UI.Page.ProcessRequest'
Step into: Stepping over non-user code 'ASP.memdonation_aspx.ProcessRequest'
Step into: Stepping over non-user code 'System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute'
Step into: Stepping over non-user code 'System.Web.HttpApplication.ExecuteStep'
Nita
  • 195
  • 3
  • 8
  • 20
  • Are you (or the user) accidentally clicking `btnSubmit` twice out of habit? Also, I edited your "to" address so the email address doesn't get added to spam lists. P.S. You forgot to do `mailMessage.Dispose()` before the redirect. – Andrew Morton Jul 10 '14 at 18:18
  • Another possibility is that you have multiple event handlers linking the button click event to this sub. Check for any dynamically added by "addhandler" calls. Don't forget that the "handles" statement on the sub created one for you automatically. – Bradley Uffner Jul 10 '14 at 18:22
  • Have you looked at the call stack? – Bjørn-Roger Kringsjå Jul 10 '14 at 18:23
  • @AndrewMorton - No I clicked only once. Thanks for changing the email address. I did add dispose statment. – Nita Jul 10 '14 at 19:09
  • @Bradley - How do I check for dynamic addhandler calls. I don't have any other events. – Nita Jul 10 '14 at 19:10
  • @Bjorn: This is how the call stack looks. Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.Send' Step into: Stepping over non-user code 'System.Web.UI.Page.Response.get' Step into: Stepping over non-user code 'MemDonation.txtAmount.get' – Nita Jul 10 '14 at 19:19
  • Apart from the possibility of having more than one event handler registered for `btnSubmit.Click`, have you confirmed that an email sent from an ordinary email program doesn't result in two emails being received? There could be some automatic forwarding going on somewhere. – Andrew Morton Jul 10 '14 at 19:24
  • Have you set the `AutoEventWireUp` page property to True? [What does AutoEventWireUp page property mean?](http://stackoverflow.com/questions/680878/what-does-autoeventwireup-page-property-mean) – Andrew Morton Jul 11 '14 at 08:29
  • @AndrewMorton: it is set to false - AutoEventWireup="false" – Nita Jul 11 '14 at 15:20
  • I suggest copying the code into a text editor to save it, deleting the button *from the designer* and the associated code, rebuild the project (to force it to look at everything), then re-creating the button and pasting back the code. If that still doesn't make it work, you could try waving a computer manual at it (any one will do as long as it is fairly thick). – Andrew Morton Jul 11 '14 at 20:26
  • @AndrewMorton - Well I tried the very big book what is available in the Library but that did not work either :). I tried my luck by tranfering the code onto the production server, but failed. I think I will leave it as such for now, get the all the other features working and then come back to it. Thanks Andrew. – Nita Jul 14 '14 at 19:30
  • You could use the browser's developer tools (I think you press F12 in IE, or use [Firebug](https://addons.mozilla.org/en-US/firefox/addon/firebug/) in Firefox) to see if the request is being sent twice:, if so, then you need to have a look at the code in the browser; if not, then the duplication happens server-side. – Andrew Morton Jul 14 '14 at 19:37
  • Well, well, well, it suddenly dawned on me. The last line in the code for page redirect has boolean value "False". If I change the value to "True", then the code won't loop for the second time and will send the email only once. – Nita Jul 14 '14 at 19:46

1 Answers1

0

You may have multiple event handlers registered for btnSubmit.Click. The Handles clause in the function will create one for you automatically. Check for any additional handlers added dynamically via addhandler.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76