Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5.1 > BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 24th, 2016, 11:29 PM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 19: Moving Application Settings to Web.config

Hi Imar,

I followed the steps in the Try It Out. However, at Step 12 after I fill out the contact form, I get an error message that an error occurred while sending the email.

At step 13, when requesting a nonexistent page in the address bar, I do not receive a "File Not Found" message. I go to a Time Warner Cable web page.
Reply With Quote
  #2 (permalink)  
Old January 25th, 2016, 09:20 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,052
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

As Ken used to say here:

>>>> the error is on line 11

>> How do you know?

Exactly.

In other words, you need to supply a lot more information such as the actual error message, how you're hosting your site, how you browse to it (the URL) etc.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old January 25th, 2016, 09:37 PM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Question Chapter 19: Moving Application Settings to Web.config

= Hi Imar,

For Chapter 19, I did a work around. I opened Chapter 19 as a website and did the subject Try It Out by verifying the coding was in accordance with the steps of the Try It Out. The only change I made was to set the value to "true" for the <add key="SendMailOnError" value="true" />.

I pressed Ctrl+F5 and filled in the contact form. Instead of receiving an email, I got an error message "An error occurred while sending your email. Please try again" at the bottom of the Contact Us page.

I believe the message came from contactForm.ascx.cs. Here is the code.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO; // Provides access to the File class for reading the file
using System.Net.Mail; // Provides access to the various mail related classes

public partial class Controls_ContactForm : System.Web.UI.UserControl
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }

  protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
  {
    if (!string.IsNullOrEmpty(PhoneHome.Text) || !string.IsNullOrEmpty(PhoneBusiness.Text))
    {
      args.IsValid = true;
    }
    else
    {
      args.IsValid = false;
    }
  }

  protected void SendButton_Click(object sender, EventArgs e)
  {
    if (Page.IsValid)
    {
      string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
      string mailBody = File.ReadAllText(fileName);

      mailBody = mailBody.Replace("##Name##", Name.Text);
      mailBody = mailBody.Replace("##Email##", EmailAddress.Text);
      mailBody = mailBody.Replace("##HomePhone##", PhoneHome.Text);
      mailBody = mailBody.Replace("##BusinessPhone##", PhoneBusiness.Text);
      mailBody = mailBody.Replace("##Comments##", Comments.Text);

      try
      {
        MailMessage myMessage = new MailMessage();
        myMessage.Subject = "Response from web site";
        myMessage.Body = mailBody;

        myMessage.From = new MailAddress(AppConfiguration.FromAddress, AppConfiguration.FromName);
        myMessage.To.Add(new MailAddress(AppConfiguration.ToAddress, AppConfiguration.ToName));
        myMessage.ReplyToList.Add(new MailAddress(EmailAddress.Text));

        SmtpClient mySmtpClient = new SmtpClient();
        mySmtpClient.Send(myMessage);

        MessageSentPara.Visible = true;
        FormTable.Visible = false;
      }
      catch (SmtpException)
      {
        Message.Text = "An error occurred while sending your e-mail. Please try again.";
      }
      finally
      {
        Message.Visible = true;
      }
    }
  }
}
The address in the address bar is "http://localhost:64882/About/Contact". When I change the address to "DefaultTest.aspx, I go to the Time Warner Cable website. I do not receive a "File Not Found" message as indicated in step 13.
Reply With Quote
  #4 (permalink)  
Old January 26th, 2016, 07:48 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,052
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

Hi there.

For the email issue, did it work with the test page you created earlier? Try removing the try/catch block so you see the real error.

About the 404 error: looks like your browser is showing a custom 404. Have you tried a different browser?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old January 26th, 2016, 11:14 PM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 19: Moving Application Settings to Web.config

For the email issue, did it work with the test page you created earlier?
I'm not sure what you mean by this question. When I created the ContactForm per the instructions in earlier chapters, the email worked. When I did Chapter 18 using my website, I did receive the following error messages:

c:\BegASPNET\Site\Controls\ContactForm.ascx.cs(57) : error CS0246: The type or namespace 'StmpException' could not be found (are you missing a using directive or an assembly reference?)

c:\BegASPNET\Site\Controls\ContactForm.ascx.cs(57, 16): error CS0246: The type or namespace 'StmpException' could not be found (are you missing a using directive or an assembly reference?)

c:\BegASPNET\Site\Controls\ContactForm.ascx.cs(59, 23: error CS0117: 'System.Net.Mail.MailMessage' does not contain the definition for 'Text'

Try removing the try/catch block so you see the real error.
I opened Chapter 19 as the website because of the errors above and the fact that I skipped Chapter 17.

I removed the try/catch/finally block and got the error message;
Line 5
Error: Sys.WebForms.PageRequestManagerParserErrorExceptio n: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to the Response.Write(), response filters, HttpModules, or server trace is enable.

Have you tried a different browser? Not before your question. I since used Goggle Chrome, and the program works.

Therefore, how do I get it to work in Internet Explorer?
Reply With Quote
  #6 (permalink)  
Old January 26th, 2016, 11:59 PM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 19: Moving Application Settings to Web.config

Please allow me change my answer to the last question:
Have you tried a different browser?
I since tried Goggle Chrome, and the Contact Us form disappears after pressing the Send button. It does not display any error message on screen. However, when I check my email, I do receive an email message titled "Error in page http://localhost:582​46/About/Contact".
Reply With Quote
  #7 (permalink)  
Old January 27th, 2016, 06:16 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,052
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

Hi there,

>> The type or namespace 'StmpException' could not be found

Looks like a typo. It should be SmtpException.

>> 'System.Net.Mail.MailMessage' does not contain the definition for 'Text'

Can you post the full code for the control and its code behind so I can take a look? The error messages you're getting don't reflect the code you posted earlier.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #8 (permalink)  
Old January 27th, 2016, 08:10 AM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 19: Moving Application Settings to Web.config

Here are the code files from my website where the error messages you are referring to came from.

ContactForm.ascx
Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactForm.ascx.cs" Inherits="Controls_ContactForm" %>
<style type="text/css">
  .auto-style1 {
    width: 100%;
  }
  .auto-style2 {
    height: 26px;
  }
  .auto-style3 {
  }
  .auto-style4 {
    width: 279px;
  }
  .auto-style7 {
    width: 279px;
    height: 26px;
  }
  .auto-style8 {
    width: 303px;
  }
  .auto-style9 {
    width: 406px;
    height: 26px;
  }
  .auto-style10 {
    width: 406px;
  }
  </style>
<script>
  function validatePhoneNumbers(source, args)
  {
    var phoneHome = document.getElementById('<%= PhoneHome.ClientID%>');
    var phoneBusiness = document.getElementById('<%= PhoneBusiness.ClientID %>');
    if (phoneHome.value != '' || phoneBusiness.value != '')
    {
      args.IsValid = true;
    }
    else
    {
      args.IsValid = false;
    }
  }
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
<div id="TableWrapper">
<table class="auto-style1" runat="server" id="FormTable" >
  <tr>
    <td class="auto-style2" colspan="3">Use the form below to get in touch with us.&nbsp; Enter your name, e-mail address, and your
      <br />
      home or business phone to get in touch with us.</td>
  </tr>
  <tr>
    <td class="auto-style10">Name</td>
    <td class="auto-style4">
      <asp:TextBox ID="Name" runat="server"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Name" CssClass="ErrorMessage" ErrorMessage="Enter you name">*</asp:RequiredFieldValidator>
    </td>
  </tr>
  <tr>
    <td class="auto-style10">E-mail address</td>
    <td class="auto-style4">
      <asp:TextBox ID="EmailAddress" runat="server" TextMode="Email"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="EmailAddress" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Enter an e-mail address">*</asp:RequiredFieldValidator>
      <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="EmailAddress" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Emter a valid e-mail address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
    </td>
  </tr>
  <tr>
    <td class="auto-style9">Repeat e-mail address</td>
    <td class="auto-style7">
      <asp:TextBox ID="ConfirmEmailAddress" runat="server" TextMode="Email"></asp:TextBox>
    </td>
    <td class="auto-style2">
      <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="ConfirmEmailAddress" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Confirm the e-mail address">*</asp:RequiredFieldValidator>
      <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="EmailAddress" ControlToValidate="ConfirmEmailAddress" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="The e-mail adresses don't match">*</asp:CompareValidator>
    </td>
  </tr>
  <tr>
    <td class="auto-style10">Home phone number</td>
    <td class="auto-style4">
      <asp:TextBox ID="PhoneHome" runat="server"></asp:TextBox>
    </td>
    <td>
      <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="validatePhoneNumbers" CssClass="ErrorMessage" Display="Dynamic" EnableTheming="True" ErrorMessage="Enter your home or business phone number" OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
    </td>
  </tr>
  <tr>
    <td class="auto-style10">Business phone number</td>
    <td class="auto-style4">
      <asp:TextBox ID="PhoneBusiness" runat="server"></asp:TextBox>
    </td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td class="auto-style10">Comments</td>
    <td class="auto-style4">
      <asp:TextBox ID="Comments" runat="server" Height="140px" TextMode="MultiLine" Width="398px"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="Comments" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Enter a comment">*</asp:RequiredFieldValidator>
    </td>
  </tr>
  <tr>
    <td class="auto-style10">&nbsp;</td>
    <td class="auto-style4">
      <asp:Button ID="SendButton" runat="server" Text="Send" OnClick="SendButton_Click" />
    </td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td class="auto-style3" colspan="3">
      <asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="ErrorMessage" HeaderText="Please correct the following errors:" />
    </td>
  </tr>
</table>
</div>
<asp:Label ID="Message" runat="server" CssClass="Attention" Text="Message Sent" Visible="False" />
    <p runat="server" id="MessageSentPara" visible="false">Thank you for your message.  We'll get in touch with you if neccesay.</p>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1" >
  <ProgressTemplate>
    <div class="PleaseWait">
      Please Wait...
    </div>
  </ProgressTemplate>
</asp:UpdateProgress>
<script>
  $(function ()
  {
    $('form').bind('submit', function ()
    {
      if (Page_IsValid)
      {
        $('#TableWrapper').slideUp(3000);
      }
    });
  });
  function pageLoad()
  {
    $('.Attention').animate({width: '600px'}, 3000).animate({width: '100px'}, 3000).fadeOut('slow');
  }
</script>

ContactForm.ascx.cs
I did have a typo in my code, and I changed it to SmtpException and the red squiggly line underneath it disappeared. However, the red squiggly line under Text in MailMessage.Text still remains.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;              // Provides access to the File class for reading the file
using System.Net.Mail;     // Provides access to the various mail related classes

public partial class Controls_ContactForm : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
      if (!string.IsNullOrEmpty(PhoneHome.Text) || !string.IsNullOrEmpty(PhoneBusiness.Text))
      {
        args.IsValid = true;
      }
      else
      {
        args.IsValid = false;
      }
    }
    protected void SendButton_Click(object sender, EventArgs e)
    {
      if (Page.IsValid)
      {
        string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
        string mailBody = File.ReadAllText(fileName);

        mailBody = mailBody.Replace("##Name##", Name.Text);
        mailBody = mailBody.Replace("##Email##", EmailAddress.Text);
        mailBody = mailBody.Replace("##HomePhone##", PhoneHome.Text);
        mailBody = mailBody.Replace("##BusinessPhone##", PhoneBusiness.Text);
        mailBody = mailBody.Replace("##Comments##", Comments.Text);

        try
        {
          MailMessage myMessage = new MailMessage();
          myMessage.Subject = "Response from web sites";
          myMessage.Body = mailBody;

          myMessage.From = new MailAddress("dojotom@gmail.com", "Dexter Tom");
          myMessage.To.Add(new MailAddress("dojotom@gmail.com", "Dexter Tom"));
          myMessage.ReplyToList.Add(new MailAddress(EmailAddress.Text));

          SmtpClient mySmtpClient = new SmtpClient();
          mySmtpClient.Send(myMessage);

          MessageSentPara.Visible = true;
          FormTable.Visible = false;
          System.Threading.Thread.Sleep(5000);
        }
        catch (SmtpException)
        {
          MailMessage.Text = "An error occurred while sending your e-mail.  Please try again.";
        }
        finally
        {
          Message.Visible = true;
        }
      }
    }
}
Reply With Quote
  #9 (permalink)  
Old January 27th, 2016, 10:31 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,052
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

>> However, the red squiggly line under Text in MailMessage.Text still remains.

Yep, because you try to set Text on the MailMessage class, not on the Message Label as you do in the Finally block:

MailMessage.Text = "An error occurred while sending your e-mail. Please try again.";

should be

Message.Text = "An error occurred while sending your e-mail. Please try again.";

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
phztfte1 (January 27th, 2016)
  #10 (permalink)  
Old January 27th, 2016, 09:23 PM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Smile Chapter 19: Moving Application Settings to Web.config

Happy! Happy! Happy!

My website is up and running. After pressing the send button, the form disappears and a message is displayed thanking the person for the message. I checked my email and I have a response from the website.

Thank you very much. It works perfectly.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Web.Config SMTP Settings vbboyd BOOK: Beginning ASP.NET 4.5 : in C# and VB 13 October 6th, 2014 03:20 PM
Question about extracting web.config settings in layered app ATCONWAY BOOK: Professional ASP.NET Design Patterns 5 October 21st, 2010 09:09 AM
Blog - Web.Config Settings for Access Database kanzeon4 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 April 5th, 2008 07:46 AM
Web.Config and Settings dsr771 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 August 13th, 2007 12:59 PM
Web.Config Settings debsoft VS.NET 2002/2003 1 December 11th, 2003 05:06 PM



All times are GMT -4. The time now is 09:34 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.