What is LINQ : Language Integrated Query

Keywords

  • Server Tools : LinqDataSource and Databinding to
  • LINQ TO SQL Classes
  • Code Behind DBML
  • Use of App_Data folder the place of dbml of LINQ to SQL class
 

 Step 1: Create a new Website

Add a database

C:\INETPUB\WWWROOT\ASPNET.35\MM.LINQ1\APP_DATA\LINQDB1.MDF

C:\inetpub\wwwroot\aspnet.35\mm.LINQ1\App_Data\LINQDB1.mdf

 

Since we did not create any connection to the database, Web.config  shows no connection string either.

After knowing the path and the database, it is time to deal with LINQ tools.

Add a new item

If we did not choose App_code as a target folder, where these objects would be installed, you will get a message as shown below.

The above action creates a view of the database table on the designer pane, as shown above. And the table is live meaning you can view the field properties by selecting the field, here selected field is FirstName and the datatype is varchar(50) and condition is not null.

Save, in case you are interested to view the code generated in LinqSql1.designer.cs, follow this link (_URL)

 

Now drag a LINQDatasource

You will notice a DataContext object is created

Click To finish, Web.config folder will add a connection string, similar to the step with SqlDataSource or ObjectDadaSource; this time you will note "Data Source=.\SQLEXPRESS;" instead of  "Data Source=ServerName\SQLEXPRESS;" using LInqDataSource.

<connectionStrings>
<add name="LINQDB1ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LINQDB1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

Now create a div element, and modifiy property as shown below, add a dropdown list there

Using it's smart tag, Choose a data source

Now set is as enable Post back.

Now add a label control and click the control, IDE will create function or method as shown below, just add a line of code to show the item selected during the run time selection.

If you try to run now, you will notice this error, just remark the name space " Linq.DataContext"

Now Run

 

Code 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>mm.LINQ1 : Default Page</title>
<style type="text/css" runat="server">
body
{
background-color:Gray;
}
#div2
{
position:absolute; padding-left:20px; padding-top:20px;
background-color: #FFFFCC; left:50px; height:300px; width:300px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="LinqtoSql1DataContext" Select="new (FirstName)"
TableName="LinqMains">
</asp:LinqDataSource>
</div>
<div id="div2">
<asp:Label ID="L1" runat="server" /><br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="LinqDataSource1" DataTextField="FirstName"
DataValueField="FirstName" Height="30px" Width="179px"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</div>
</form>
</body>
</html>

Code Behind DBML: (DataBase MarkUP Language

<?xml version="1.0" encoding="utf-8"?>
<Database Name="LINQDB1" Class="LinqtoSql1DataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
<Connection Mode="WebSettings" ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LINQDB1.mdf;Integrated Security=True;User Instance=True" SettingsObjectName="System.Configuration.ConfigurationManager.ConnectionStrings" SettingsPropertyName="LINQDB1ConnectionString" Provider="System.Data.SqlClient" />
<Table Name="dbo.LinqMain" Member="LinqMains">
<Type Name="LinqMain">
<Column Name="Lid" Type="System.String" DbType="NChar(10) NOT NULL" IsPrimaryKey="true" CanBeNull="false" />
<Column Name="FirstName" Type="System.String" DbType="VarChar(50) NOT NULL" CanBeNull="false" />
<Column Name="LastName" Type="System.String" DbType="VarChar(50) NOT NULL" CanBeNull="false" />
<Column Name="City" Type="System.String" DbType="VarChar(50) NOT NULL" CanBeNull="false" />
<Column Name="Position" Type="System.String" DbType="VarChar(50) NOT NULL" CanBeNull="false" />
</Type>
</Table>
</Database>