 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

January 25th, 2016, 12:29 AM
|
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
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.
|
|

January 25th, 2016, 10:20 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

January 25th, 2016, 10:37 PM
|
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
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.
|
|

January 26th, 2016, 08:48 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

January 27th, 2016, 12:14 AM
|
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
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?
|
|

January 27th, 2016, 12:59 AM
|
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
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:58246/About/Contact".
|
|

January 27th, 2016, 07:16 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

January 27th, 2016, 09:10 AM
|
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
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. 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> </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"> </td>
<td class="auto-style4">
<asp:Button ID="SendButton" runat="server" Text="Send" OnClick="SendButton_Click" />
</td>
<td> </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("[email protected]", "Dexter Tom");
myMessage.To.Add(new MailAddress("[email protected]", "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;
}
}
}
}
|
|

January 27th, 2016, 11:31 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
>> 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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

January 27th, 2016, 10:23 PM
|
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
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.
|
|
 |
|