Tuesday, January 26, 2010

LINQ Examples

Here is the code from the class. Rember, for any of this to work, you must use the server Explorer window, create a connection to Community Assist Database. Add a new LINQ to SQL Classes object to your project and drag the Service, Person and PersonGrant tables into the designer.


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 db As New CAssistDataContext
Dim caService = (From s In db.Services _
Order By s.ServiceName _
Select s.ServiceKey, s.ServiceName).Distinct


DropDownList1.DataSource = caService
DropDownList1.DataTextField = "ServiceName"
DropDownList1.DataValueField = "ServiceKey"
DropDownList1.DataBind()
End If
End Sub

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

Dim db As New CAssistDataContext
Dim caGrant = From g In db.ServiceGrants _
Order By g.GrantDate _
Where g.ServiceKey = DropDownList1.SelectedValue _
Select g.Person.LastName, g.Person.FirstName, g.GrantDate, g.GrantAmount


DataList1.DataSource = caGrant
DataList1.DataBind()



End Sub
End Class


<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>
</div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<hr />
<p>
<asp:Label ID="lblLastname" runat="server" Text='<%#Eval("LastName") %>'></asp:Label>,
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
<br />
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("GrantDate") %>'></asp:Label>
<br />
<asp:Label ID="lblGrantAmount" runat="server" Text='<%#Eval("GrantAmount") %>'></asp:Label>
</p>
<hr />
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>

No comments:

Post a Comment