Tuesday, February 26, 2013

ADO Entities

Remember you have to add an ADO.Net Data Entities object to your web site. We called in "CommunityAssistModel". Add the tables Person, PersonAddress, PersonContact, Donation and Service.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ul>
        <asp:DataList ID="DataList1" runat="server">
            <ItemTemplate>
                <li><strong><%#Eval("ServiceName") %> </strong>:
                     <%#Eval("ServiceDescription") %></li>
            </ItemTemplate>
        </asp:DataList>
        </ul>
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">make a Donation</asp:LinkButton>
    </div>
    </form>
</body>
</html>


Default.aspx.cs

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CommunityAssistEntities cae = new CommunityAssistEntities();
        var serv = from s in cae.Services
                   orderby s.ServiceName
                   select new { s.ServiceName, s.ServiceDescription };
        DataList1.DataSource = serv.ToList();
        DataList1.DataBind();

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default2.aspx");
    }
}

Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>Enter Last Name</td>
                <td>
                    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Enter first Name</td>
                <td>
                    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Enter Street Address</td>
                <td>
                    <asp:TextBox ID="txtStreet" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>City</td>
                <td>
                    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>State (2 characters)</td>
                <td>
                    <asp:TextBox ID="txtState" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Enter Zip Code</td>
                <td>
                    <asp:TextBox ID="txtZip" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Enter Home Phone</td>
                <td>
                    <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Enter Email</td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Enter Donation Amount</td>
                <td>
                    <asp:TextBox ID="txtDonation" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label></td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /> </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>


Default2.aspx.cs

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)
    {
        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");

    }
}

No comments:

Post a Comment