Preview:
# FULL GUIDE — Using LINQ to Query Data from Database with ADO.NET

We’ll build a working project called StudentDBApp that connects SQL Server + ASP.NET Web Forms + LINQ to SQL.

PART 1: Install SQL Server & SSMS (Database Setup)

Step 1: Download SQL Server

1. Go to Microsoft’s official page: [https://www.microsoft.com/en-in/sql-server/sql-server-downloads](https://www.microsoft.com/en-in/sql-server/sql-server-downloads)
2. Go to Downloads Tab at top
3. Download SQL Server 2022 Developer
4. Choose Basic Installation (wait a long time)

Step 2: Install SQL Server Management Studio (SSMS)

1. Go to: [https://aka.ms/ssms](https://aka.ms/ssms)
2. Click Download SQL Server Management Studio (SSMS)
3. Install it normally and open after installation.

Step 3: Connect to Server
When SSMS opens:
1. Server type: Database Engine
2. Server name: localhost
3. Authentication: Windows Authentication
4. Click trust
5. Click Connect

You’re now connected to your local SQL Server.

Step 4: Create Database and Table
In SSMS → open New Query and paste this SQL:

CREATE DATABASE StudentDB;
GO

USE StudentDB;
GO

CREATE TABLE Students (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(50),
StudentDept VARCHAR(50),
StudentMarks DECIMAL(5,2)
);

INSERT INTO Students VALUES (1, 'Rahul', 'CSE', 92.5);
INSERT INTO Students VALUES (2, 'Raj', 'ECE', 85.0);
INSERT INTO Students VALUES (3, 'Anu', 'IT', 88.0);

Press Execute.
You now have a StudentDB database and Students table with data.

PART 2: Setup Visual Studio

Step 1: Install Required Components
If you already have Visual Studio 2022:

1. Open Visual Studio Installer
2. Click Modify on your Visual Studio version
3. Go to Workloads tab and make sure these are checked:

   * .NET desktop development
   * ASP.NET and web development
4. Go to Individual Components tab

   * Search and check “LINQ to SQL tools”
5. Click Modify and let it install.

This adds LINQ to SQL support (.dbml files).

PART 3: Create the ASP.NET Project

Step 1: New Project

1. Open Visual Studio 2022
2. Go to File → New → Project
3. Choose: ASP.NET Web Application (.NET Framework)
4. Name it StudentDBApp
5. Click Create
6. Select Empty template and check Web Forms
7. Click Create

Step 2: Add a Web Form

1. In Solution Explorer, right-click the project → Add → Web Form
2. Name it StudentDisplay.aspx
3. Click Add

Step 3: Add LINQ to SQL Class

1. Right-click your project → Add → New Item
2. Select Data (left side)
3. Choose LINQ to SQL Classes
4. Name it StudentData.dbml
5. Click Add
   You’ll see a designer window open.

Step 4: Connect to SQL Server

1. Open Server Explorer (View → Server Explorer)
2. Right-click Data Connections → Add Connection
3. Fill:
   Server name: localhost
   Authentication: Windows Authentication
   Click trust
   Database name: StudentDB
4. Click Test Connection → OK

Connection established.

Step 5: Add Table to Designer

1. In Server Explorer, expand your connection → Tables
2. Drag the Students table into the StudentData.dbml designer.
   It automatically creates the Student class.
   Save the file (Ctrl + S).

PART 4: Design and Code

Step 1: Design the Frontend (ASPX)
Open StudentDisplay.aspx → inside <form runat="server">, add:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" />

Save it.

Step 2: Backend Code (C#)
Open StudentDisplay.aspx.cs → replace code with:

using System;
using System.Linq;
using System.Configuration;

namespace StudentDBApp
{
public partial class StudentDisplay : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowStudentData();
}
}

    private void ShowStudentData()  
    {  
        string connString = ConfigurationManager.ConnectionStrings["StudentDBConnectionString"].ConnectionString;  
        StudentDataDataContext db = new StudentDataDataContext(connString);  
        var studentDetails = from s in db.Students  
                             select s;  
        GridView1.DataSource = studentDetails;  
        GridView1.DataBind();  
    }  
}  


}

Save it.

Step 4: Set Start Page
In Solution Explorer, right-click StudentDisplay.aspx → Set as Start Page

Step 5: Run the Project
Press Ctrl + F5
Your browser opens:
[https://localhost:[port]/StudentDisplay.aspx](https://localhost:[port]/StudentDisplay.aspx)

You’ll see the GridView displaying all student records from SQL Server.

OUTPUT (Example)

StudentID | StudentName | StudentDept | StudentMarks
1 | Rahul | CSE | 92.50
2 | Raj | ECE | 85.00
3 | Anu | IT | 88.00
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter