using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlfredoLotar.Livro.Aspnet.SchoolModel;
using System.Data;
using System.Drawing;

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

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        using (SchoolEntities contexto = new SchoolEntities())
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(txtNome.Text.Trim()) &&
                    !string.IsNullOrWhiteSpace(txtSobrenome.Text.Trim()))
                {
                    Person p = new Person();
                    p.FirstName = txtNome.Text.Trim();
                    p.LastName = txtSobrenome.Text.Trim();
                    p.HireDate = DateTime.Now;
                    p.EnrollmentDate = DateTime.Now.AddYears(1);
                    contexto.AddToPerson(p);
                    contexto.SaveChanges();
                    Preencher();
                }
                else
                {
                    Label1.Text = "Preencha todos os campos.";
                    Label1.ForeColor = Color.Red;
                }
                
            }
            catch (EntitySqlException ex)
            {
                Response.Write("Erro EntitySqlException");
            }
            catch (EntityCommandExecutionException ex)
            {
                Response.Write("Erro EntityCommandExecutionException");
            }
        }
    }

    void Preencher()
    {
        using (SchoolEntities contexto = new SchoolEntities())
        {
            GridView1.DataSource = contexto.ExecuteStoreQuery<Person>
                    ("Select top 10 * from Person order by PersonID DESC");
            GridView1.DataBind();
        }
    }    
}