๐Ÿ—ƒ๏ธ ASP.NET Data Handling & Integration
Estimated reading: 4 minutes 39 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

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

Or Copy Link

CONTENTS
Scroll to Top