Wrox Programmer Forums
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
|
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 February 17th, 2016, 09:50 AM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 19: AppConfiguration does not contain a definition for FromAddress

Here is the code from my ContactForm.ascx.cs file. There is a red squiggly line under the boldfaced FromAddress.
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);

        Message.Visible = true;
        MessageSentPara.Visible = true;
        FormTable.Visible = false;
      }
      catch (SmtpException)
      {
        Message.Text = "An error occurred while sending your email.  Please try again.";
      }
      finally
      {
        Message.Visible = true;
      }
    }
  }
}
Here is my Web.config code. I x'd out my password.
Code:
<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
    <add key="FromAddress" value="[email protected]" />
    <add key="FromName" value="Dexter Tom" />
    <add key="ToAddress" value="[email protected]" />
    <add key="ToName" value="Dexter Tom" />
    <add key="SendMailOnError" value="true" />
  </appSettings>
  <system.web>
    <customErrors mode="On" defaultRedirect="~/Errors/OtherErrors.aspx" redirectMode="ResponseRewrite">
      <error statusCode="404" redirect="~/Errors/Error404.aspx" />
    </customErrors>
    <pages theme="Monochrome" styleSheetTheme="Monochrome"  />
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
  </system.web>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="Dexter Tom &lt;[email protected]&gt;">
        <network enableSsl="true" host="smtp.gmail.com" password="xxxxxxxx" userName="[email protected]" port="587" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

Here is the code from my AppConfiguration.cs file.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;

/// <summary>
/// Summary description for AppConfiguration
/// </summary>
public class AppConfiguration
{
	public static string FromAddresss
	{
    get
    {
      string result = WebConfigurationManager.AppSettings.Get("FromAddress");
      if (!string.IsNullOrEmpty(result))
      {
        return result;
      }
      throw new Exception("AppSetting FromAddress not found in web.config file.");
    }
	}

  public static string FromName
  {
    get
    {
      string result = WebConfigurationManager.AppSettings.Get("FromName");
      if (!string.IsNullOrEmpty(result))
      {
        return result;
      }
      throw new Exception("AppSetting FromName not found in web.config file.");
    }
  }

  public static string ToAddress
  {
    get
    {
      string result = WebConfigurationManager.AppSettings.Get("ToAddress");
      if (!string.IsNullOrEmpty(result))
      {
        return result;
      }
      throw new Exception("AppSetting ToAddress not found in web.config file.");
    }
  }

  public static string ToName
  {
    get
    {
      string result = WebConfigurationManager.AppSettings.Get("ToName");
      if (!string.IsNullOrEmpty(result))
      {
        return result;
      }
      throw new Exception("AppSetting ToName not found in web.config file.");
    }
  }

  public static bool SendMailOnError
  {
    get
    {
      string result = WebConfigurationManager.AppSettings.Get("SendMailOnError");
      if (!string.IsNullOrEmpty(result))
      {
        return Convert.ToBoolean(result);
      }
      throw new Exception(
                        "AppSetting SendMailOnError not found in web.config file.");
    }
  }
}
Here is my Global.asax file.
Code:
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Net.Mail" %>
<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
      RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
      ScriptManager.ScriptResourceMapping.AddDefinition("jquery",
        new ScriptResourceDefinition
        {
          Path = "~/Scripts/jquery-2.2.0.min.js"
        }
      );
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
      
    }

    void Application_Error(object sender, EventArgs e)
    {
      if (AppConfiguration.SendMailOnError)
      {
        // Code that runs when an unhandled error occurs
        if (HttpContext.Current.Server.GetLastError() != null)
        {
          Exception myException = HttpContext.Current.Server.GetLastError().GetBaseException();
          string mailSubject = "Error in page " + Request.Url.ToString();
          string message = string.Empty;
          message += "<strong>Message</strong><br />" + myException.Message + "<br />";
          message += "<strong>Stack Trace</strong><br />" + myException.StackTrace + "<br />";
          message += "<strong>Query String</strong><br />" + Request.QueryString.ToString() + "<br />";
          MailMessage myMessage = new MailMessage(AppConfiguration.FromAddresss, AppConfiguration.ToAddress, mailSubject, message);
          myMessage.IsBodyHtml = true;
          SmtpClient mySmtpClient = new SmtpClient();
          mySmtpClient.Send(myMessage);
        }
      }
    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }
</script>
The program worked before the Moving Application Settings to Web.config Try It Out.

I am a few Try It Outs away from publishing my first web site. Please help.
 
Old February 17th, 2016, 11:06 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Count the number of s's in AppConfiguration.FromAddresss. There are more of them in the property definition than in the calling code.

Which makes me wonder if you aren't using Intelli Sense? Wouldn't you have picked the right property name, despite the misspelling?

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 February 17th, 2016, 11:16 PM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 19: AppConfiguration does not contain a definition for FromAddress

Hi Imar,

The errors were corrected and the programs runs.

With regard to the use of Intelli Sense, I often see what I want when I am almost completed with typing the word, so I complete the typing instead. Sometimes what I want is several selections below what is highlighted. So, rather than grab the mouse to make the selection, I keep on typing.

What I need to get better at is finding my errors. I knew it had to do with FromAddress. But, believe it or not, I looked at all the files that had to be modified in the Try It Out for FromAddress. The errors completely blew by me.

But since you pointed out the extra 's', I used Quick Find to find all occurrences and that's how I found the two errors. It may be a better and faster method than going through the code manually.

Again, thank you very much.
 
Old February 18th, 2016, 03:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi again,

>> Sometimes what I want is several selections below what is highlighted. So, rather than grab the mouse to make the selection, I keep on typing.

You can use the up and down arrow keys to select an item and then press enter to select the item. Faster than using the mouse indeed, and a lot faster than making typos first ;-)

>> I used Quick Find to find all occurrences and that's how I found the two errors. It may be a better and faster method than going through the code manually.

In case like this, you need to build the site (from the Build or Website menu) and then take a look at the Error List which then shows you what's wrong and where.

Hope this helps,

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!
The Following User Says Thank You to Imar For This Useful Post:
phztfte1 (February 18th, 2016)
 
Old February 18th, 2016, 08:53 AM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Smile Chapter 19: AppConfiguration does not contain a definition for FromAddress

Hi Imar,

Wow!!! Your last post certainly desires a thumbs up. It will help me make less errors and find those errors rather than going through the code line-by-line, word-by-word.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 19: First Try It Out phztfte1 BOOK: Beginning ASP.NET 4.5.1 : in C# and VB 3 January 31st, 2016 03:46 PM
Chapter 19 smloomis BOOK: Beginning ASP.NET 4 : in C# and VB 4 June 29th, 2013 04:13 AM
Chapter 19 RuthL BOOK: JavaScript 24-Hour Trainer 3 July 10th, 2012 07:42 AM
AppConfiguration Legusol BOOK: Beginning ASP.NET 4 : in C# and VB 5 April 15th, 2011 03:05 AM
AppConfiguration Class AlexW BOOK: Beginning ASP.NET 4 : in C# and VB 1 April 9th, 2011 10:46 AM





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