Ahmad+Bilal+&+Abnerson+Malivert

Objective
toc

The objective of this report is to add new capabilities to the Acts of Love website. This project will cover the topic of adding a login page, a registration page, an email page and adding a database to the Acts of Love website. In the first part of the project, I covered the topics of html and styling with cascade style sheets (CSS). The requirements for the website are:
 * To have a way for people to register and become members of the site.
 * For the users to be able to login to the Acts of Love website.
 * To be able to send email to the Acts of Love Founders.

1.0 Adding Database to Acts of Love website
In order to meet the requirements of registering members and enabling user log in forms, we need a way to hold user info. I decided to add a database to the Acts of love website. To add a table to an ASP.NET website, you must:
 * Right click on the website in the explorer window and click on new items.
 * Click on the SQL server database and name it appropriately.
 * I like to use LINQ instead of SQL to search through a database. To achieve that goal, I added a LINQ to SQL classes. This Class will connect to the table in my database and will give me a way to access that table without writing SQL scripts. Looking at the image below, you can go through the first column on the left and see the SQL database and the LINQ to SQL class items.




 * I added a table called UserInfo in my database. This table will hold the user’s full name, username, email address, gender, age, and password. Image below shows the table that I created with all the above fields, their data type and if they allow null values or not. I decided to make the username to be the key for my table, since it was one of the fields where I couldn’t allow duplicates or a null value.




 * To link the LINQ to SQL class to the UserInfo table. Open the LINQ to SQL class, and then drag the UserInfo table from the server explorer window to the UserInfo.dbml window. The image below shows what it should look like after completing this step. Now you can use the userinfo.dml to access the UserInfo table. Now I have a database and a way to access the table in it.



2.0 Adding registration form to Acts of Love website
The reason for adding a database was to have a place to save the user information when they register to the Acts of Love website. Now we need an interface for the user to enter their information on the website. To achieve that goal I needed to construct a form that will take all ask the user info to enter all the info needed to fill in the field from the table. To add the user registration from, you need:
 * Follow the 1st step of section 1 and add the user web control item to the website. If you look at the image under step3, it is the 1st item on the last column from the left. Name the web user control appropriately.
 * Open the web user control, go to the toolbox window and drag the table control to web user control design view window. The image below shows the table in the web user control window.




 * Go to the properties window to set the width of the table, the table ID and etc…
 * Go to the misc section of the table properties window, and select the row menu to add rows to the table.
 * At the row menu, click on the add button to add rows to the table. For the Acts of Love website contact form I will need 8 rows. Go to the Id property and name each one of them appropriately, this will make editing the table easier. The image below shows 8 rows in the contact form table.




 * Go to table row 0 and select the cells menu to add columns to the table. For the Acts of Love contract form, we will need 3 columns. One column for the title, the second column for the control and the third column for validation method. The picture below shows the cells menu. Go to table row 1 to 7 and repeat the same steps from table row 0.




 * Go to the Text field of table row zero and type what you want the title of each column. The image from step 6 shows what I typed for Text field for table cell 0 of table row 0. The image below shows what the table should look like after completing this step. The ### represents empty cells.



To finish designing the Contact form table, we have to add controls to the 2nd table cells, then add client side validation controls to the 3rd table cells. The Next section goes through each table row code.

1.0 Full Name Row
The full name row is where the user enters their full name. In previous section, we already had input the text " full name" in table cell 0 of the full name row. To add a text box for user input, go to the second asp:TableCell div, then drag the textbox control from the toolbox window in between it and should have a asp:TextBox div in between the asp:TableCell. Since the user has to fill out the Full Name row, we have to add a RequiredFieldValidator to the 3rd asp:TableCell div by dragging the RequiredFieldValidator to it. The code below shows the result of completing the above instructions. You have to set the Errormessage, the Text, and the ControlToValidate settings accordingly code format="asp"  Full Name        code

2.0 User Name Row
The User name row is where the user enters the username of their choice. The table cell 0 is already set when we were building the table. Repeat the instruction from the full name section to add a text box in the second table cell. The username row is also a required field, so add a RequiredFieldValidator to the third table cell. The code below is the result from following the previous instructions. code format="asp"  User Name  <asp:TextBox ID="UserNameTxt" runat="server"></asp:TextBox> </asp:TableCell> <asp:TableCell runat="server"> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter a Username" Display="Static" ControlToValidate="UserNameTxt" Text="*"> </asp:RequiredFieldValidator> </asp:TableCell> </asp:TableRow> code

3.0 Email Row
The Email row is where the user will enter his/hers email address. Like the previous row, we added a text box control to the 2nd table cell. The email row not only needs a RequiredFieldValidator because it is a required field, it also needs a RegularExpressValidator to make sure that the email address entered is a valid formatted email. The validation expression is what tells it what to look for. The code below shows what each setting should be set to. code format="asp"  <asp:TableCell ID="TableCell1" runat="server">Email</asp:TableCell> <asp:TableCell ID="TableCell2" runat="server"> <asp:TextBox ID="EmailAddressTxt" runat="server"></asp:TextBox> </asp:TableCell> <asp:TableCell ID="TableCell3" runat="server"> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please enter a valid e-mail address" Display="Static" Text="*" ControlToValidate="EmailAddressTxt"> </asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Please enter a valid e-mail address" Display="Static" Text="*" ControlToValidate="EmailAddressTxt" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator> </asp:TableCell> </asp:TableRow> code

4.0 Password Row
The Password row is where the user enters their desired password. Add a text box control to the second table cell and set the TextMode to password so that it can mask the user input. Add a RequiredFieldValidator to the 3rd table cell since it is a required field. code format="asp"  <asp:TableCell runat="server">Password</asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox ID="PasswordTxt" TextMode="Password" runat="server"></asp:TextBox></asp:TableCell> <asp:TableCell runat="server"> <asp:Label ID="PaswordLbl" runat="server" Text=""></asp:Label> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please Enter an Email Address" Display="Static" Text="*" ControlToValidate="PasswordTxt" ></asp:RequiredFieldValidator> </asp:TableCell> </asp:TableRow>

code

5.0 Re Password Row
The Re Password is used to make sure that the user remembered the password in the Password row. Add a text box to the second table cell. The difference between the Password and the Re Password row is that you will need a CompareValidator that will compare the text between the password textbox and the rePassword textbox. code format="asp"  <asp:TableCell runat="server">Re Password</asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox ID="RePassTxt" TextMode="Password" runat="server"></asp:TextBox></asp:TableCell> <asp:TableCell runat="server"> <asp:Label ID="RePasswordLbl" runat="server" Text=""></asp:Label> <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Please Confirm the email address" Display="Static" Text="*" ControlToValidate="RePassTxt" ControlToCompare="PasswordTxt"></asp:CompareValidator> </asp:TableCell> </asp:TableRow> code

6.0 Gender Row
The Gender row is where the user can enter their gender. The gender row is optional, so we do not need any validators, and also using a dropdown list in the second table cell controls what the user can input into the row. code format="asp"  <asp:TableCell runat="server">Gender</asp:TableCell> <asp:TableCell runat="server"> <asp:DropDownList ID="GenderDropDownList" runat="server">

</asp:DropDownList> </asp:TableCell> <asp:TableCell runat="server"> <asp:Label ID="GenderLbl" runat="server" Text=""></asp:Label> </asp:TableCell> </asp:TableRow> code

7.0 Age Row
The Age row is where the user can enter their age. The Age row is optional, but we do need a RegularExpressionValidator to make sure that the user enters only numerical values. The code below shows how to construct the Age row.

code format="asp"  <asp:TableCell runat="server">Age</asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox ID="AgeTxt" runat="server"></asp:TextBox> </asp:TableCell> <asp:TableCell runat="server"> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" Display="Static" Text="*" ControlToValidate="AgeTxt"  runat="server" ErrorMessage="Invalid Input" ValidationExpression="[0-9]+"> </asp:RegularExpressionValidator> </asp:TableCell> </asp:TableRow> code

8.0 Save Button Row
The save button row is where the save button is located. Drag a button control to the 1st table cell, and set the OnClick method to call the SaveBtn_Click. Later on, we will add code to the SaveBtn_Click method. Add a label control to the second table cell. The label will be use to let the user know if they have been successfully registered. code format="asp"  <asp:TableCell runat="server"> <asp:Button ID="SaveBtn" runat="server" Text="Save" OnClick="SaveBtn_Click"/> </asp:TableCell> <asp:TableCell runat="server" HorizontalAlign="Right"> <asp:Label ID="Label1" runat="server"></asp:Label> </asp:TableCell> <asp:TableCell runat="server" HorizontalAlign="Left">

</asp:TableCell> </asp:TableRow> code

The image below shows the design view of the contact form control after inputting all the code above.

9.0 Bringing the Contact Form Control to the Acts of Love Website

To finalize this part of the project, I have to integrate the contact form control with the Acts Of Love website. To achieve this goal, I needed to :

code format="html4strict" <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" Title="Register Users" %> <%@ Register src="ContactForm.ascx" tagname="ContactForm" tagprefix="uc1" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <ul> <li> Register
 * Add a new form to the website called Register and make sure that the select master page option is checked.
 * Go to the new form, and drag the contact form in between the li div html code. The uc1: ContactForm and the register header is added to the code. The picture below shows the final design.

</li>

<uc1:ContactForm ID="ContactForm1" runat="server" />

</ul> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> </asp:Content>

code



2.1 Coding The Contact Form Control
The Final Part of the Contact Form control is to go to the ContactForm.ascx.cs file and code the SaveBtn_Click. The code below is from the ContactForm.ascx.cs file. When the page first load,

>
 * 1) I set the items in the Gender drowdownlist control.
 * 2) The SaveBtn_Click method assigns all the textbox value to local strings and integers and call the AddNewUsers method.
 * 3) Use a try and catch block to catch the username already exist SQL exception, when the exception triggers, the label in the savebtn row text will let the user know what the issue is.
 * 4) Set the connection string to where the database is located.
 * 5) Userinfodatacontext is created from the LINQ to SQL class that we created in the previous section. Instantiate the UserInfoDataContext to connect to the database.
 * 6) Instantiate the UserTable to access each field in the table and set the value to those from the contact form control.
 * 7) Submit all the changes and set the label in the savebtn row to show the successful result.

code format="csharp" using System; using System.Collections; using System.Configuration; using System.Data; using System.Data.Linq; using System.Data.Sql; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Text; using System.Web;

public partial class ContactForm : System.Web.UI.UserControl {   string Strname; string StrUserName; string StrEmailAddress; string StrPassword; string StrGender; string StrAge;

protected void Page_Load(object sender, EventArgs e)   {

GenderDropDownList.Items.Add("M"); GenderDropDownList.Items.Add("F");

}

protected void SaveBtn_Click(object sender, EventArgs e)   { Strname = FullNameTxt.Text; StrUserName = UserNameTxt.Text; StrEmailAddress = EmailAddressTxt.Text; StrPassword = PasswordTxt.Text; StrGender = GenderDropDownList.Text; StrAge = AgeTxt.Text; AddNewUsers(Strname, StrUserName, StrEmailAddress, StrPassword, StrGender, StrAge);

}   protected void AddNewUsers(string FullName, string UserName, string Email, string Password,string Gender, string Age) {       try {

// add new user to the UserTable in the UserDatabase string connectionString = @"C:\Users\Administrator\Documents\ASP.NET Programs\AOLwebsite\App_Data\Database.mdf"; UserInfoDataContext context = new UserInfoDataContext(connectionString); UserInfo usertable = new UserInfo; usertable.Full_Name = FullName; usertable.User_Name = UserName; usertable.Email = Email; usertable.Password = Password; usertable.Gender = Gender; usertable.Age = Convert.ToInt32(Age); context.UserInfos.InsertOnSubmit(usertable); context.SubmitChanges; Label1.Text = "Successfully registered"; }       catch (Exception ex) {           Label1.Text = "UserName already exists"; }

}

}

code

1.0 Test 1
Test 1 is to check the required validation method in all the textboxes in the contact form. The image below shows a successful result. The red star shows that the validation where set, for the text box that where left blank and they were not set for those that were blank but didn't have the requiredfieldvalidator.



2.0 Test 2
Test 2 is to check the RegularExpressionValidator in the email textbox. I entered an email that's in the wrong format to check if it would raise a flag. The image below shows a successful result. The red star by the email textbox shows that the input is incorrect.



3.0 Test 3
Test 3 is to check the RegularExpressionValidator in the Age textbox. I added a string instead of an integer in the age textbox. The image below shows a successful result. The red star next to the Age textbox shows that the input is incorrect.



4.0 Test 4
Test 4 is to check the CompareValidator in the the re Password text box. I entered different a different string in the RePassword. The image below shows a successful test 3. The red star next to the Re Password indicates a wrong user input.



5.0 Test 5
Test 5 is to check the savebtn_click method by click the save button and check whether it successfully added a new user to the user table. The images below show a successful result. The first image shows the savebtn label displaying a successful registration. The second image shows the user record in the user table.





6.0 Test 6
Test 6 is to check the try and catch exception handler by trying to add a new user with a username that's already in the database. The image below shows a successful result, the savebtn label displays the right warning message to the user.



**4.0 Login to the Acts of Love website**
It’s really important to know who the main users are and even if we trust them completely, they are often not the only users that can access the website. In the Acts of Love website, only registered users can access the website by entering their username and password to the login page.

First a form is created called login.ascx which will ask for username and password and it will verify that information with user information saved on database.
 * The web user control item is added to the website and it is named as login.ascx appropriately.
 * The table control from toolbox is dragged to web user control design view window.
 * The table width and table ID is then selected and three rows are added to the table under misc section in properties.
 * The RequiredFieldValidator is added to the table appropriately with its properties such as error message, text and controlToValidate set appropriately.
 * In the last row menu, the send button is added along with the label which will let the user know whether the login is valid or invalid as shown below.



1.0 Username
The username row is where the user enters their registered username. In the first table cell, text “username” is added; next in the second asp:TableCell div, the textbox control is dragged and placed in between It which should have a asp:Textbox div in between the asp:TextCell. The RequiredFieldValidator is added to the third table cell by dragging it from the toolbox under validation which will ensure the user has entered a username.

code format="css" <asp:Table ID="Table1" runat="server" Height="34px" Width="502px">  <asp:TableCell runat="server">username</asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox ID="Usernametext" runat="server"></asp:TextBox></asp:TableCell> <asp:TableCell runat="server"> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter your name" Display="Static" text="*" ControlToValidate="Usernametext"></asp:RequiredFieldValidator></asp:TableCell> </asp:TableRow> code

2.0 Password
The visitor will have to enter their correct password to be able to gain access and this will ensure only the registered users are allowed. The table cell 0 is already established for the username and the same process is repeated by adding a second table cell. This is also a required field and therefore RequiredFieldValidator is added to the third table cell as shown below:

code format="css"  <asp:TableCell runat="server">password</asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox ID="PasswordTxt" TextMode="Password" runat="server"></asp:TextBox></asp:TableCell> <asp:TableCell runat="server"> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter the password" Display="Static" text="*" ControlToValidate ="PasswordTxt"></asp:RequiredFieldValidator></asp:TableCell> </asp:TableRow> code

3.0 Login button
This is where the username and password will be verified. A button control is dragged into a first table cell of the third row with its OnClick method set to call the LoginBtn_Click. A label control is added to the third table cell to let the user know if they have entered a valid username and password. code format="css"  <asp:TableCell ID="TableCell1" runat="server"> <asp:Button ID="LoginBtn" runat="server" Text="Login" OnClick="LoginBtn_Click"/> </asp:TableCell> <asp:TableCell ID="TableCell2" runat="server" HorizontalAlign="Right"> <asp:Label ID="Label1" runat="server"></asp:Label> </asp:TableCell> <asp:TableCell ID="TableCell3" runat="server" HorizontalAlign="Left"> </asp:TableCell> </asp:TableRow> code

**4.1 Bringing the Login form control to the Acts of Love Website**
To bring the login form control to the website, the following two steps are done:
 * First a new form called login Page is added to the website with the master page option selected.
 * In this step, login form control is dragged from the solution explorer and placed in between li div html code where uc1: login and login form is added to the code as shown below under Markup view of email form control.

code format="html4strict" <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="loginpage.aspx.cs" Inherits="loginpage" Title="Untitled Page" %> <%@ Register src="login.ascx" tagname="login" tagprefix="uc1" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <ul> <li> User_Login

</li>

<uc1:login ID="login1" runat="server" />

</ul> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> </asp:Content>

code



**4.2 Coding the Login Form**
The login.ascx.cs file contains the main code for the send button and when the send button is pressed by the user, the code will check if the information entered by the user is either valid or invalid. 1) First the database.mdf is located where the valid information is stored. 2) A new UserInfoData Context is created and a query statement which looks in context.UserInfos table which include the user information and it selects the user for that item. 3) A foreach loop is used to go through UserData and the username and password will be added to the dictionary as shown below: code format="csharp" using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.Sql; using System.Text; using System.Data.Linq; using System.Collections.Generic;

public partial class login : System.Web.UI.UserControl {   string StrUserName; string StrPassword;

protected void Page_Load(object sender, EventArgs e)   {

}

protected void LoginBtn_Click(object sender, EventArgs e)   { StrUserName = Usernametext.Text; StrPassword = PasswordTxt.Text; validuser(StrUserName, StrPassword); if (validuser(StrUserName, StrPassword)) Label1.Text = "login successful"; else Label1.Text = "username and password does not match"; }

protected bool validuser(string username, string password)

{

string connectionString = @"C:\Users\Bilal\Documents\Visual Studio 2008\WebSites\AOLwebsite\App_Data\Database.mdf"; UserInfoDataContext context = new UserInfoDataContext(connectionString); Dictionary<string, string> bufferUsers = new Dictionary<string, string>;

var Userdata =

from user in context.UserInfos where user.User_Name == username select user;

foreach (var user in Userdata)

bufferUsers.Add(user.User_Name, user.Password);

if (bufferUsers.ContainsKey(username)) {                   // return password.Equals(bufferUsers[username.ToLowerInvariant]); return password.Equals(bufferUsers[username]);

}

return false; }

}

code

1.0 Test 1
The following users are currently registered to the website and their information is stored at the database as shown below:





2.0 Test 2
Test 2 will check if the user can gain access to the website by entering invalid username and once the login button is pressed, the following result is obtained:

3.0 Test 3
Test 3 will check if the user can gain access to the website by entering invalid password and once the login button is pressed, the following result is obtained:



4.0 Test 4
Test 4 will check again if the user can gain access to the website by entering valid username and password and once the login button is pressed, the following result is obtained:



**5.0 Sending E-mail to the Acts of Love Founders**
Visitors will have an option of expressing their notions about the website content by sending their comments to the Acts of Love founder s directly from the website. To be able to send email from the web site, first the table is created and organized appropriately as shown in figure 5-1 below:
 * Figure 5-1 Design View of Contact form**

In above figure 5-1, the table is organized with three cells where the first cell of the row identify the area such as, From, Subject and Comments; the second cell adds text box and the third cell adds the RequiredFieldValidator from the tool box under validation. These validation controls are very helpful in validating the data that a user enters in the system. The ValidationSummary is added at the end which is used to provide feedback to the visitor about any errors made in the page.

1.0 From
This is where visitors enter their information about themselves and in this row error message, text and control to validate properties are modified for RequiredFieldValidator as shown below in Markup view. code format="css" From <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtName" ErrorMessage="Please enter your email address">*</asp:RequiredFieldValidator> code

2.0 Subject
The subject row is where the visitors can write some information about what they are going to discuss in their comment section. The error message, text and control validate properties are modified again for RequiredFieldValidator which is located in the last cell of the second row as shown in figure 5-1. code format="css" Subject <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtSubject" Display="Dynamic" ErrorMessage="Please enter your subject">*</asp:RequiredFieldValidator> code

3.0 Comment
This is where visitors will write their message for the Acts of Love founders in this row and the text is created in second cell where id name is modified for this section as shown below: code format="css" Comment <textarea id="txtComments"  runat="server" style="height: 71px; width: 241px" cols ="10" rows="10" > code

**5.1 Bringing the Email Form Control to the Acts of Love Website**
To bring the email form control to the website, the following two steps are done:
 * First a new form called Email Form Page is added to the website with the master page option selected.
 * In this step, email form control is dragged from the solution explorer and placed in between li div html code where uc1: EmailFormControl and email form is added to the code as shown below under Markup view of email form control.

code format="css" <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="EmailFormPage.aspx.cs" Inherits="EmailFormPage" Title="Untitled Page" %> <%@ Register src="EmailFormControl.ascx" tagname="EmailFormControl" tagprefix="uc1" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <ul> <li> Email_Form

</li>

<uc1:EmailFormControl ID="EmailFormControl1" runat="server" Visible="True" />

</ul> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> </asp:Content> code



**5.2 Coding The Email Form Control**
The EmailFormControl.ascx.cs file contains the main code for the send button and when the send button is pressed by the visitor, the following information is utilized before the comments are sent to the founders:
 * 1) First namespace called System.Net.Mail is added to the code which contains number of objects that create and send email messages.
 * 2) Next the following three objects MailMessage which contain properties such as Subject and Body to set the message contents is utilized; The MailAddress represents a receiver address used in the email and SmtpClient Is used to send the actual message where it checks the SMTP server and in this case smtp. west.cox.net is utilized.
 * 3) The Founders email address is included in the code so that email goes directly to the appropriate address.
 * 4) A try and catch block is used to catch the email sending error exception and when the exception triggers, the label in figure 5-1 is used to let the visitor know about the issue.

code format="csharp" using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; //using System.Web.Mail; using System.Net.Mail;

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

}   protected void btnSend_Click(object sender, EventArgs e)    { try {           MailMessage msg = new MailMessage;

//msg.To = "bilal58@hotmail.com"; msg.To.Add("bilal58@hotmail.com");

msg.From = new MailAddress(txtName.Text);

msg.Subject = txtSubject.Text;

msg.Body = txtComments.Value; Label1.Text = "Sending...";

SmtpClient smtpMail = new SmtpClient("smtp.west.cox.net"); smtpMail.Send(msg);

Label1.Text = "Sent email (" + txtSubject.Text + ") to " + "bilal58@hotmail.com"; }       catch (Exception ) {           Label1.Text = " Sending email error"; }

} }

code

1.0 Test 1


When visitor press the button, the message will be sent to the email and the status will be displayed to the visitor.





2.0 Test 2
If the visitor accidently press the send button without any information added to the form, the text property from RequiredFieldValidator will appear along with ValidationSummary which will display the error message property from the RequiredFieldValidator as shown below:



3.0 Test 3
If the user forgets to include an @ symbol in their email address, then the try and catch block will handle the exception. The image below shows a successful result by displaying a message to the user.