Binding to a DataReader - Demo

 

Notice that the ObjectDataSource control here includes two properties named 'TypeName' and 'SelectMethod'. The TypeName property contains the name of the component that you want to represent with the ObjectDataSource control. The SelectMethod represents the method of the component that you want to call when selecting data.

*** MovieDataReader.vb ***

Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration


Public Class MovieDataReader
    Private ReadOnly _conString As String

    Public Function GetMovies() As SqlDataReader
        Dim con As New SqlConnection(_conString)

        Dim cmd As New SqlCommand()
        cmd.Connection = con
        cmd.CommandText = "SELECT ID, Title, Director FROM Movies"

        con.Open()
        Return cmd.ExecuteReader(CommandBehavior.CloseConnection)

    End Function

    Public Sub New()
        _conString = WebConfigurationManager.ConnectionStrings("MyDatabase").ConnectionString
    End Sub

End Class

*** ShowMovieDataReader.aspx ***

<%@ Page Language="VB" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Show Movie Data Reader</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grdMovies"
        DataSourceId="srcMovies" 
        runat="server" />
        
        <asp:ObjectDataSource ID="srcMovies" 
        TypeName="MovieDataReader"
        SelectMethod="GetMovies"
        runat="server" />
    
    </div>
    </form>
</body>
</html>