doc_mm.LINQ3b.htm <>LINQ : Entity


  • LINQ Entity
  • SqlDataSource
  • Details View
  • private string strcnn = WebConfigurationManager.ConnectionStrings["LINQ3BConnection"].ConnectionString;
 Image talks thousand lines:


Step 1. Created a new website ( Intranet User http://manas6/aspnet.35/mm.LINQ3b/default.aspx )

added a database and a table

Step 2: Edited the class

using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
/// <summary>
/// Summary description for Linq_Entity
/// </summary>
[Table(Name = "dbo.Simple_Table")]
public class Linq_Entity
/* public Linq_Entity()
// TODO: Add constructor logic here
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public string Lid { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Position { get; set; }
public string Experience { get; set; }
public string Notes { get; set; }

Step 3: Created a connection string, using SQLDataSoutrce

That registered the string in web config file, note the difference, here we are using file database than initial catalog used in the previous document.

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

Step 4: creating user's interface


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
<title>mm.LINQ3B : LINQ Entity Data Context </title>
<style type="text/css">
position:absolute; height:200px; width:300px;
left:30px; top:20px; padding-left:20px; padding-top:20px;
background-color: #FFFFCC;
<form id="form1" runat="server">
<div id="div1">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LINQ3BConnection %>"
SelectCommand="SELECT * FROM [Simple_Table]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" Height="32px" Width="192px"
DataSourceID="SqlDataSource1" DataTextField="FirstName" AutoPostBack="true"
</asp:DropDownList> <br />
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="288px"
Caption="Details View (Using Linq Entity)" BorderColor="#996600"
BorderStyle="Groove" BorderWidth="6px">

Code Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//configuration manager
using System.Web.Configuration;
// DataContext
using System.Data.Linq;
public partial class _Default : System.Web.UI.Page
private string strcnn = WebConfigurationManager.ConnectionStrings["LINQ3BConnection"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
DataContext db = new DataContext(strcnn);
var tlinq = db.GetTable<Linq_Entity>();
DetailsView1.DataSource = tlinq.Where(m => m.FirstName.Equals(DropDownList1.SelectedItem.Text));
//DetailsView1.DataSource = tlinq.Where(m => m.FirstName.Contains(DropDownList1.SelectedItem.Text));

Step 5: Runtime analysis, opens blank with no data in detail view.

Dropdown list with LINQ entity.