Tuesday, May 24, 2011

LINQ Queries and Writing to Database

First add a LINQ to SQL classes designer to your web site.

Drag the tables you need from the Solution Explorer onto the LINQ Designer



Here is the code for the simple form to add a magazine to the database.

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>New Magazine</h1>
<asp:Label ID="Label1" runat="server" Text="Enter Magazine Name"></asp:Label>
<asp:TextBox ID="txtMagazineName" runat="server"></asp:TextBox><br />
<asp:DropDownList ID="ddlMagazineType" runat="server">
</asp:DropDownList><br />
<asp:DropDownList ID="ddlSubscriptionType" runat="server">
</asp:DropDownList><br />
<asp:Label ID="Label2" runat="server" Text="Enter Price"></asp:Label>
<asp:TextBox ID="txtPrice" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>

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
{
//instantiate the data context
MagazineDataDataContext dc = new MagazineDataDataContext();

protected void Page_Load(object sender, EventArgs e)
{
//make sure it only happens on the original post of the page
if (!IsPostBack)
{
//call the methods that fill the drop down lists
FillMagazineType();
FillSubscriptionType();
}


}
protected void Button1_Click(object sender, EventArgs e)
{
//instantiate the Magazine Class. It is mapped
//to the magazine table in the database
Magazine mag = new Magazine();

//assign values to the magazine instance
mag.MagName = txtMagazineName.Text;
mag.MagType = ddlMagazineType.SelectedItem.ToString();

//mark it for insert on submit
dc.Magazines.InsertOnSubmit(mag);

MagazineDetail md = new MagazineDetail();
md.Magazine = mag; //tie it to the magazine we just made
md.SubscriptTypeID = int.Parse(ddlSubscriptionType.SelectedValue.ToString());
md.SubscriptionPrice = decimal.Parse(txtPrice.Text);

dc.MagazineDetails.InsertOnSubmit(md);

dc.SubmitChanges(); //submit the rows to the database
}

private void FillMagazineType()
{

//a linq query to get the magazine type
//it uses Distinct because in the magazines table
//several different magazines have the same types
//distinct keeps it from repeating
var mtype = (from t in dc.Magazines
orderby t.MagType
select new { t.MagType }).Distinct();

//bind the result of the query to the control
ddlMagazineType.DataSource = mtype.ToList();
ddlMagazineType.DataTextField = "MagType";
ddlMagazineType.DataBind();
}

private void FillSubscriptionType()
{

//LINQ query gets the fields from SubscriptionType
var subType = from s in dc.SubscriptionTypes
orderby s.SubscriptTypeName
select new { s.SubscriptTypeID, s.SubscriptTypeName };

ddlSubscriptionType.DataSource = subType.ToList();
ddlSubscriptionType.DataTextField = "SubscriptTypeName";
ddlSubscriptionType.DataValueField = "SubscriptTypeID";
ddlSubscriptionType.DataBind();
}
}

No comments:

Post a Comment