here is our example using LINQ
Remember you need to add a LINQ to SQL designer to your project and the drag the tables from the Server Explorer onto the Designer. Then be sure to save the Designer so that it will generate the classes that you need.
The LINQ Designer
The first thing we did was to simple load the services into a dataList. Here is the code behind
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) { CommunityAssistDataContext dc = new CommunityAssistDataContext(); var serv = from s in dc.Services orderby s.ServiceName select new { s.ServiceName, s.ServiceDescription }; /* var donors = from d in dc.Donations select new { d.Person.FirstName, d.Person.LastName, d.DonationDate} */ DataList1.DataSource = serv.ToList(); DataList1.DataBind(); } protected void LinkButton1_Click(object sender, EventArgs e) { Response.Redirect("Donate.aspx"); } }
Here is the source for Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h1>Services Offered</h1> <p> <asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <strong><asp:Label ID="Label1" runat="server" Text='<%#Eval("ServiceName") %>'></asp:Label></strong>, <asp:Label ID="Label2" runat="server" Text='<%#Eval("ServiceDescription") %>'></asp:Label> <br /> </ItemTemplate> </asp:DataList> </p> <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Donate?</asp:LinkButton> </div> </form> </body> </html>
Here is the Source for Donate.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Donate.aspx.cs" Inherits="Donate" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td> <asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label></td> <td> <asp:TextBox ID="txtFirstname" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label></td> <td> <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Label ID="Label3" runat="server" Text="Street"></asp:Label></td> <td> <asp:TextBox ID="txtStreet" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Label ID="Label4" runat="server" Text="City"></asp:Label></td> <td> <asp:TextBox ID="txtCity" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Label ID="Label5" runat="server" Text="State"></asp:Label></td> <td> <asp:TextBox ID="txtState" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Label ID="Label6" runat="server" Text="Zip Code"></asp:Label></td> <td> <asp:TextBox ID="txtZipCode" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Label ID="Label7" runat="server" Text="Phone"></asp:Label></td> <td> <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Label ID="Label8" runat="server" Text="Email"></asp:Label></td> <td> <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Label ID="Label9" runat="server" Text="Donation"></asp:Label></td> <td> <asp:TextBox ID="txtDonation" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Button ID="Button1" runat="server" Text="Donate" onclick="Button1_Click" /> </td> <td> <asp:Label ID="lblResult" runat="server" Text=""></asp:Label> </td> </tr> </table> </div> </form> </body> </html>
Here is the code behind for Donate.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 Donate : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { CommunityAssistDataContext dc = new CommunityAssistDataContext(); Person p = new Person(); p.FirstName = txtFirstname.Text; p.LastName = txtLastName.Text; dc.Persons.InsertOnSubmit(p); PersonAddress pa = new PersonAddress(); pa.Person = p; pa.Street = txtStreet.Text; pa.City = txtCity.Text; pa.State = txtState.Text; pa.Zip = txtZipCode.Text; dc.PersonAddresses.InsertOnSubmit(pa); PersonContact phone = new PersonContact(); phone.Person = p; phone.ContactInfo = txtPhone.Text; phone.ContactTypeKey = 1; dc.PersonContacts.InsertOnSubmit(phone); PersonContact email = new PersonContact(); email.Person=p; email.ContactInfo=txtEmail.Text; email.ContactTypeKey=6; dc.PersonContacts.InsertOnSubmit(email); Donation d = new Donation(); d.Person = p; d.DonationAmount = decimal.Parse(txtDonation.Text); d.DonationDate = DateTime.Now; d.EmployeeKey = 1; dc.Donations.InsertOnSubmit(d); dc.SubmitChanges(); lblResult.Text = "Thank you for your Donation"; } }
No comments:
Post a Comment