Tuesday, August 10, 2010

LINQ with Stored Procedure ASP

Here is the code for the asp web page we did using linq. Remember you have to add the Link to SQL. Drag the person and donation tables onto the form and the usp_Mydonations onto the method part of the window.

here is 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>
<:asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<:/asp:DropDownList>
<:asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
GridLines="None">
<:FooterStyle BackColor="Tan" />
<:PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<:SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<:HeaderStyle BackColor="Tan" Font-Bold="True" />
<:AlternatingRowStyle BackColor="PaleGoldenrod" />
<:/asp:GridView>
<:/div>
<:/form>
<:/body>
<:/html>

Here is default.aspx.cs

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

public partial class _Default : System.Web.UI.Page
{
DataClassesDataContext dc = new DataClassesDataContext();

protected void Page_Load(object sender, EventArgs e)
{
var donors = (from d in dc.Donations
select new {d.Person.LastName, d.PersonKey }).Distinct();

if (!IsPostBack)
{

DropDownList1.DataSource = donors.ToList();
DropDownList1.DataTextField = "Lastname";
DropDownList1.DataValueField = "PersonKey";
DropDownList1.DataBind();

}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Response.Write(DropDownList1.SelectedValue.ToString());
//Response.Write(DropDownList1.SelectedItem.ToString());
GridView1.DataSource=dc.usp_myDonations(int.Parse(DropDownList1.SelectedValue.ToString())).ToList();
GridView1.DataBind();

}
}

No comments:

Post a Comment