Thursday, February 28, 2013

Assignment six with Validation code

I am only posting the code behind for Default2.aspx.cs because that is where we made all our changes. We added some validation for the fields and a try catch for the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int isErrors=Validator();
        if (isErrors == 0)
        {
            try
            {
                CommunityAssistEntities cae = new CommunityAssistEntities();
                Person p = new Person();
                p.LastName = txtLastName.Text;
                p.FirstName = txtFirstName.Text;
                cae.People.Add(p);

                PersonAddress pa = new PersonAddress();
                pa.Person = p;
                pa.Street = txtStreet.Text;
                pa.City = txtCity.Text;
                pa.State = txtState.Text;
                pa.Zip = txtZip.Text;
                cae.PersonAddresses.Add(pa);

                PersonContact pc = new PersonContact();
                pc.Person = p;
                pc.ContactInfo = txtPhone.Text;
                pc.ContactTypeKey = 1;
                cae.PersonContacts.Add(pc);

                PersonContact pc2 = new PersonContact();
                pc2.Person = p;
                pc2.ContactInfo = txtEmail.Text;
                pc2.ContactTypeKey = 6;
                cae.PersonContacts.Add(pc2);

                Donation d = new Donation();
                d.Person = p;
                d.DonationDate = DateTime.Now;
                d.DonationAmount = decimal.Parse(txtDonation.Text);
                cae.Donations.Add(d);

                cae.SaveChanges();

                Response.Redirect("Default3.aspx");
            }
            catch (ArgumentNullException en)
            {
                string msg = en.Message;
                CreateAlert(msg);
            }
            catch (Exception ex)
            {

                string msg = ex.Message;
                CreateAlert(msg);
            }
        }//end if

    }//end method

    private int Validator()
    {
        int errors = 0;
         string msg;

        if (txtLastName.Text.Equals(""))
        {
            msg = "last name is required";
            CreateAlert(msg);
            errors = 1;
            
        }
        
        decimal amount=0;
        //try parse tests the text to see if it can be turned into a number
        //if no returns false, if true returns true and assigns the value to the 
        //out parameter
        bool isDonation = decimal.TryParse(txtDonation.Text, out amount);
        if (!isDonation)
        {
            msg = "You must enter a numerical donation";
            CreateAlert(msg);
            errors = 1;
        }

        if (txtState.Text.Length != 2)
        {
            msg="Please enter a 2 character state abrv.";
            CreateAlert(msg);
            errors = 1;
        }

        return errors;
    }

    private void CreateAlert(string msg)
    {
        Response.Write("<script type='text/JavaScript'>alert('" + msg + "')</script>");
    }
}

No comments:

Post a Comment