Wrox Programmer Forums
|
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
 
Old January 29th, 2016, 01:11 AM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 19: First Try It Out

Hi Imar,

My happiness was short lived. Since my website work, I decided to use it for Chapter 19. I ran into a problem doing so with the Try It Out: Moving Application Settings to Web.config.

At step 12, I used Internet Explorer to open the homepage and received an error message:

An exception of type 'System.Exception' occurred in App_Code.nye4pf4x.dll but was not handled in user code

Additional information: AppSetting To Address not found in the web.config file.

If there is a handler for this exception, the program may be safely continued.

Here is my ContactForm.ascx.cs 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 sites";
          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;
        }
      }
    }
}
Here is my web.config code.
Code:
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="PlanetWroxConnectionString1" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\PlanetWrox.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="PlanetWroxEntities" connectionString="metadata=res://*/App_Code.PlanetWrox.csdl|res://*/App_Code.PlanetWrox.ssdl|res://*/App_Code.PlanetWrox.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\PlanetWrox.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
    <add key="FromAddress" value="[email protected]" />
    <add key="FromName" value="Planet Wrox" />
    <add key="ToAdress" value="[email protected]" />
    <add key="ToName" value="Planet Wrox" />
    <add key="SendMailOnError" value="true" />
  </appSettings>
  <system.web>
    <trace mostRecent="true" enabled="false" requestLimit="100" pageOutput="false" localOnly="true" />
    <customErrors mode="On" defaultRedirect="~/Errors/OtherErrors.aspx" redirectMode="ResponseRewrite">
      <error statusCode="404" redirect="~/Errors/Error404.aspx" />
    </customErrors>
    <authentication mode="Forms">
      <forms loginUrl="~/Login" />
    </authentication>
    <pages theme="Monochrome">
      <controls>
        <add tagPrefix="Wrox" tagName="Banner" src="~/Controls/Banner.ascx" />
      <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" /></controls>
    </pages>
    <compilation debug="true" targetFramework="4.5.1">
      <assemblies>
        <add assembly="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <add assembly="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
      <buildProviders>
        <add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider" />
      </buildProviders>
    </compilation>
    <httpRuntime targetFramework="4.5.1" />
    <profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="PlanetWroxConnectionString1" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="PlanetWroxConnectionString1" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="DefaultRoleProvider">
      <providers>
        <add connectionStringName="PlanetWroxConnectionString1" applicationName="/" name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </providers>
    </roleManager>
    <!--
            If you are deploying to a cloud environment that has multiple web server instances,
            you should change session state mode from "InProc" to "Custom". In addition,
            change the connection string named "DefaultConnection" to connect to an instance
            of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
      -->
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="PlanetWroxConnectionString1" />
      </providers>
    </sessionState>
  </system.web>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="Dexter Tom &lt;[email protected]&gt;">
        <network enableSsl="true" host="smtp.gmail.com" password="(Se1026An)" userName="[email protected]" port="587" />
      </smtp>
    </mailSettings>
  </system.net>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="NameServiceAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="NameService">
        <endpoint address="" behaviorConfiguration="NameServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="NameService" />
      </service>
    </services>
  </system.serviceModel>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <location path="Management">
    <system.web>
      <authorization>
        <allow roles="Managers" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
I also ran it using Goggle Chrome. I did not receive any error message, but the Contact Us form scrolls up and does not provide any thank you message. Also, no email is sent.

Please help resolve.
 
Old January 29th, 2016, 11:27 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

>> Additional information: AppSetting To Address not found in the web.config file.

Is the space between To And Address present in your code? Or is that a formatting issue in this forum?

Either way, you may want to count the number of d's in ToAddress in your web.config.

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!
 
Old January 29th, 2016, 10:42 PM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Smile Chapter 19: First Try It Out

Thank you, Imar; the program works perfectly.

I don't know how you do it, but you're great at it. I printed out the web.config file from Chapter 19 and matched it against my web.config file and didn't find the error. I also retraced my steps in the Try It Out.

The space between the To and Address is my typo.

Again, thank you very much. I appreciate the time you took to find the error.
 
Old January 31st, 2016, 03:46 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome; glad to hear it's working now.

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Example in the Chapter 19 [email protected] BOOK: Professional Team Foundation Server 2013 0 December 22nd, 2015 02:30 PM
Next Chapter after 19? AlanWheeler BOOK: Beginning ASP.NET 4.5 : in C# and VB 1 February 15th, 2014 10:40 AM
Chapter 19 smloomis BOOK: Beginning ASP.NET 4 : in C# and VB 4 June 29th, 2013 04:13 AM
Chapter 19 johanyu BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 3 November 12th, 2007 10:50 AM
Chapter 19 johanyu BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 October 10th, 2004 11:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.