๐Ÿ—ƒ๏ธ ASP.NET Data Handling & Integration
Estimated reading: 4 minutes 198 views

ASP.NET โ€“ Data Sources โ€“ Connect, Bind, and Display Data Easily (With Code Explanation)


Introduction โ€“ What Are Data Sources in ASP.NET?

Data Source Controls in ASP.NET provide a declarative and code-free way to fetch, bind, insert, update, and delete data from external data stores like:

  • SQL Server
  • XML Files
  • Objects and Business Layers

They work with controls like GridView, DropDownList, ListView, etc., and eliminate the need for manual ADO.NET boilerplate code.

In this guide, you’ll learn:

  • How to use SqlDataSource, AccessDataSource, and ObjectDataSource
  • How to bind them to UI controls like GridView
  • Full examples with line-by-line explanations and output

ASP.NET Data Source Overview

ControlUsed WithBackend Type
SqlDataSourceGridView, DropDownList, etc.SQL Server or OLEDB
AccessDataSourceGridView, ListBox, etc.Microsoft Access
ObjectDataSourceAny bindable controlCustom C# classes
XmlDataSourceTreeView, Menu, etc.XML files

Example 1: SqlDataSource with GridView

Goal: Display Users table from SQL Server without writing C# code.


Step 1: DbDataSourceDemo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DbDataSourceDemo.aspx.cs" Inherits="DbDataSourceDemo" %>

<html>
<body>
  <form id="form1" runat="server">

    <h3> SQL Data Source Demo</h3>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:MyDb %>"
        SelectCommand="SELECT * FROM Users">
    </asp:SqlDataSource>

    <asp:GridView ID="GridView1" runat="server"
        DataSourceID="SqlDataSource1" AutoGenerateColumns="true">
    </asp:GridView>

  </form>
</body>
</html>

Code Explanation

LineWhat It Does
<asp:SqlDataSource />Connects to SQL Server and runs a query
ConnectionString="<%$ ConnectionStrings:MyDb %>"Uses the named connection string from web.config
SelectCommand="SELECT * FROM Users"Selects all records from the Users table
DataSourceID="SqlDataSource1"Binds the GridView to the SqlDataSource
AutoGenerateColumns="true"Displays all database columns automatically

Output Preview in Browser

 SQL Data Source Demo

+----+------------+---------------------+
| Id | FullName   | Email               |
+----+------------+---------------------+
| 1  | John Doe   | john@example.com    |
| 2  | Jane Smith | jane@example.com    |
+----+------------+---------------------+

The GridView automatically displays all data from your database.


Example 2: Insert, Update, Delete with SqlDataSource

Add these attributes to SqlDataSource1:

InsertCommand="INSERT INTO Users (FullName, Email) VALUES (@FullName, @Email)"
UpdateCommand="UPDATE Users SET FullName=@FullName, Email=@Email WHERE Id=@Id"
DeleteCommand="DELETE FROM Users WHERE Id=@Id"

And set DataKeyNames="Id" in the GridView:

<asp:GridView ID="GridView1" runat="server" 
              DataSourceID="SqlDataSource1" 
              AutoGenerateEditButton="true"
              AutoGenerateDeleteButton="true"
              DataKeyNames="Id">
</asp:GridView>

Explanation

AttributeDescription
AutoGenerateEditButton="true"Adds an โ€œEditโ€ button to each row
AutoGenerateDeleteButton="true"Adds a โ€œDeleteโ€ button
InsertCommand/UpdateCommand/DeleteCommandSQL commands used internally for operations

This enables full CRUD (Create, Read, Update, Delete) operations without C# code.


Example 3: AccessDataSource with Access DB

<asp:AccessDataSource ID="AccessDataSource1" runat="server"
    DataFile="~/App_Data/Users.mdb"
    SelectCommand="SELECT * FROM Users">
</asp:AccessDataSource>

<asp:GridView ID="GridView2" runat="server" 
    DataSourceID="AccessDataSource1" AutoGenerateColumns="true">
</asp:GridView>

Similar to SqlDataSource, but works with .mdb Access files.


Example 4: ObjectDataSource with C# Class

Step 1: Create a C# class

public class UserManager
{
    public List<User> GetAllUsers()
    {
        return new List<User>
        {
            new User { Id = 1, FullName = "Sam Blue", Email = "sam@example.com" },
            new User { Id = 2, FullName = "Alex Green", Email = "alex@example.com" }
        };
    }
}

public class User
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; }
}

Step 2: Use ObjectDataSource in ASPX

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    TypeName="UserManager"
    SelectMethod="GetAllUsers">
</asp:ObjectDataSource>

<asp:GridView ID="GridView3" runat="server"
    DataSourceID="ObjectDataSource1" AutoGenerateColumns="true">
</asp:GridView>

Explanation

AttributeDescription
TypeNameThe C# class name to instantiate
SelectMethodThe method to call that returns a collection
DataSourceIDBinds to GridView

Great for MVC-style separation using your own business layer.


Summary โ€“ Recap & Takeaways

Key Learnings:

  • SqlDataSource is ideal for rapid DB binding with SQL Server
  • AccessDataSource works well for small .mdb databases
  • ObjectDataSource connects to custom C# classes
  • These controls make UI and data bind seamlessly without code
  • Full CRUD support is possible declaratively

Perfect for fast prototyping and admin panels in Web Forms projects.


Frequently Asked Questions (FAQs)


Can I bind a DropDownList to SqlDataSource?
Yes! Just set:

DataSourceID="SqlDataSource1"
DataTextField="FullName"
DataValueField="Id"

Is SqlDataSource secure for production apps?
Itโ€™s fast for internal apps. For public apps, use Parameter controls to avoid SQL Injection.


Can I combine SqlDataSource with custom code?
Yes. You can override or extend its use in code-behind if needed.


Can I use Stored Procedures with SqlDataSource?
Absolutely! Just set SelectCommandType="StoredProcedure" and pass parameters.


Share Now :
Share

๐Ÿ“ฅ ASP.NET โ€“ Data Sources

Or Copy Link

CONTENTS
Scroll to Top