Thursday, April 22, 2010

LINQ to SQL

Here is the code. Remember to do this you need to go to WebSite/Add New Item/LINQ SQL/ and drag the Person and Donor tables onto the LINQ Designers

Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>
<br />
<!--Here is the beginning of the datalist -->
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Donor Name"></asp:Label>
<asp:Label ID="Label2"
runat="server" Text='<%#Eval("LastName") %>'></asp:Label><br />

<asp:Label ID="Label3" runat="server" Text="Date"></asp:Label>
<asp:Label ID="Label4"
runat="server" Text='<%#Eval("DonationDate") %>'></asp:Label><br />

<asp:Label ID="Label5" runat="server" Text="Amount"></asp:Label>
<asp:Label ID="Label6"
runat="server" Text='<%#Eval("DonationAmount") %>'></asp:Label>

</ItemTemplate>
</asp:DataList>



</div>
</form>
</body>
</html>

Default.Aspx.vb


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then
Dim dc As New DataClassesDataContext
Dim donor = (From p In dc.Donations _
Order By p.Person.LastName Ascending _
Select p.PersonKey, p.Person.LastName).Distinct

DropDownList1.DataSource = donor
DropDownList1.DataTextField = "LastName"
DropDownList1.DataValueField = "PersonKey"
DropDownList1.DataBind()
End If


End Sub

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim dc As New DataClassesDataContext
Dim contributions = From d In dc.Donations _
Order By d.DonationDate Ascending _
Where d.PersonKey = DropDownList1.SelectedValue _
Select d.Person.LastName, d.DonationDate, d.DonationAmount

DataList1.DataSource = contributions
DataList1.DataBind()

End Sub
End Class

No comments:

Post a Comment