Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
User Action:
.NET Framework Class Library

DataSourceView Class

Serves as the base class for all data source view classes, which define the capabilities of data source controls.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Public MustInherit Class DataSourceView

The DataSourceView type exposes the following members.

  Name Description
Protected method DataSourceView Initializes a new instance of the DataSourceView class.
Top
  Name Description
Public property CanDelete Gets a value indicating whether the DataSourceView object associated with the current DataSourceControl object supports the ExecuteDelete operation.
Public property CanInsert Gets a value indicating whether the DataSourceView object associated with the current DataSourceControl object supports the ExecuteInsert operation.
Public property CanPage Gets a value indicating whether the DataSourceView object associated with the current DataSourceControl object supports paging through the data retrieved by the ExecuteSelect method.
Public property CanRetrieveTotalRowCount Gets a value indicating whether the DataSourceView object associated with the current DataSourceControl object supports retrieving the total number of data rows, instead of the data.
Public property CanSort Gets a value indicating whether the DataSourceView object associated with the current DataSourceControl object supports a sorted view on the underlying data source.
Public property CanUpdate Gets a value indicating whether the DataSourceView object associated with the current DataSourceControl object supports the ExecuteUpdate operation.
Protected property Events Gets a list of event-handler delegates for the data source view.
Public property Name Gets the name of the data source view.
Top
  Name Description
Public method CanExecute Determines whether the specified command can be executed.
Public method Delete Performs an asynchronous delete operation on the list of data that the DataSourceView object represents.
Public method Equals(Object) Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method ExecuteCommand(String, IDictionary, IDictionary) Executes the specified command.
Public method ExecuteCommand(String, IDictionary, IDictionary, DataSourceViewOperationCallback) Executes the specified command.
Protected method ExecuteDelete Performs a delete operation on the list of data that the DataSourceView object represents.
Protected method ExecuteInsert Performs an insert operation on the list of data that the DataSourceView object represents.
Protected method ExecuteSelect Gets a list of data from the underlying data storage.
Protected method ExecuteUpdate Performs an update operation on the list of data that the DataSourceView object represents.
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as the default hash function. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Insert Performs an asynchronous insert operation on the list of data that the DataSourceView object represents.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnDataSourceViewChanged Raises the DataSourceViewChanged event.
Protected method RaiseUnsupportedCapabilityError Called by the RaiseUnsupportedCapabilitiesError method to compare the capabilities requested for an ExecuteSelect operation against those that the view supports.
Public method Select Gets a list of data asynchronously from the underlying data storage.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Update Performs an asynchronous update operation on the list of data that the DataSourceView object represents.
Top
  Name Description
Public event DataSourceViewChanged Occurs when the data source view has changed.
Top

ASP.NET supports a data-binding architecture that enables Web server controls to bind to data in a consistent fashion. Web server controls that bind to data are referred to as data-bound controls, and the classes that facilitate that binding are called data source controls. Data source controls can represent any data source: a relational database, a file, a stream, a business object, and so on. Data source controls present data in a consistent way to data-bound controls, regardless of the source or format of the underlying data.

You use the data source controls that are provided with ASP.NET, including SqlDataSource, AccessDataSource, and XmlDataSource, to perform most Web development tasks. You use the base DataSourceControl and DataSourceView classes when you want to implement your own custom data source control.

You can think of a data source control as the combination of the IDataSource object and its associated lists of data, called data source views. Each list of data is represented by a DataSourceView object. The DataSourceView class is the base class for all data source views, or lists of data, associated with data source controls. Data source views define the capabilities of a data source control. Because the underlying data storage contains one or more lists of data, a data source control is always associated with one or more named data source views. The data source control uses the GetViewNames method to enumerate the data source views currently associated with the data source control and the GetView method to retrieve a specific data source view instance by name.

All DataSourceView objects support data retrieval from the underlying data source using the ExecuteSelect method. All views optionally support a basic set of operations, including operations such as ExecuteInsert, ExecuteUpdate, and ExecuteDelete. A data-bound control can discover the capabilities of a data source control by retrieving an associated data source view using the GetView and GetViewNames methods, and by querying the view at design time or run time.

The following code example demonstrates how to extend the DataSourceView class to create a strongly typed view class for a data source control. The CsVDataSourceView class defines the capabilities of the CsvDataSource data source control and provides an implementation for data-bound controls to use data stored in comma-separated value (.csv) files. For more information on the CsvDataSource data source control, see the DataSourceControl class.

' The CsvDataSourceView class encapsulates the 
' capabilities of the CsvDataSource data source control. 

Public Class CsvDataSourceView
   Inherits DataSourceView

   Public Sub New(owner As IDataSource, name As String)
       MyBase.New(owner, DefaultViewName)
   End Sub 'New 

   ' The data source view is named. However, the CsvDataSource 
   ' only supports one view, so the name is ignored, and the 
   ' default name used instead. 
   Public Shared DefaultViewName As String = "CommaSeparatedView" 

   ' The location of the .csv file. 
   Private aSourceFile As String = [String].Empty

   Friend Property SourceFile() As String 
      Get 
         Return aSourceFile
      End Get 
      Set 
         ' Use MapPath when the SourceFile is set, so that files local to the 
         ' current directory can be easily used. 
         Dim mappedFileName As String
         mappedFileName = HttpContext.Current.Server.MapPath(value)
         aSourceFile = mappedFileName
      End Set 
   End Property 

   ' Do not add the column names as a data row. Infer columns if the CSV file does 
   ' not include column names. 
   Private columns As Boolean = False 

   Friend Property IncludesColumnNames() As Boolean 
      Get 
         Return columns
      End Get 
      Set
         columns = value
      End Set 
   End Property 

   ' Get data from the underlying data source. 
   ' Build and return a DataView, regardless of mode. 
   Protected Overrides Function ExecuteSelect(selectArgs As DataSourceSelectArguments) _
    As System.Collections.IEnumerable
      Dim dataList As IEnumerable = Nothing 
      ' Open the .csv file. 
      If File.Exists(Me.SourceFile) Then 
         Dim data As New DataTable()

         ' Open the file to read from. 
         Dim sr As StreamReader = File.OpenText(Me.SourceFile)

         Try 
            ' Parse the line 
            Dim dataValues() As String 
            Dim col As DataColumn

            ' Do the following to add schema.
            dataValues = sr.ReadLine().Split(","c)
            ' For each token in the comma-delimited string, add a column 
            ' to the DataTable schema. 
            Dim token As String 
            For Each token In dataValues
               col = New DataColumn(token, System.Type.GetType("System.String"))
               data.Columns.Add(col)
            Next token

            ' Do not add the first row as data if the CSV file includes column names. 
            If Not IncludesColumnNames Then
               data.Rows.Add(CopyRowData(dataValues, data.NewRow()))
            End If 

            ' Do the following to add data. 
            Dim s As String 
            Do
               s = sr.ReadLine()
               If Not s Is Nothing Then
                   dataValues = s.Split(","c)
                   data.Rows.Add(CopyRowData(dataValues, data.NewRow()))
               End If 
            Loop Until s Is Nothing 

         Finally
            sr.Close()
         End Try

         data.AcceptChanges()
         Dim dataView As New DataView(data)
         If Not selectArgs.SortExpression Is String.Empty Then
             dataView.Sort = selectArgs.SortExpression
         End If
         dataList = dataView
      Else 
         Throw New System.Configuration.ConfigurationErrorsException("File not found, " + Me.SourceFile)
      End If 

      If dataList is Nothing Then 
         Throw New InvalidOperationException("No data loaded from data source.")
      End If 

      Return dataList
   End Function 'ExecuteSelect


   Private Function CopyRowData([source]() As String, target As DataRow) As DataRow
      Try 
         Dim i As Integer 
         For i = 0 To [source].Length - 1
            target(i) = [source](i)
         Next i
      Catch iore As IndexOutOfRangeException
         ' There are more columns in this row than 
         ' the original schema allows.  Stop copying 
         ' and return the DataRow. 
         Return target
      End Try 
      Return target
   End Function 'CopyRowData

   ' The CsvDataSourceView does not currently 
   ' permit deletion. You can modify or extend 
   ' this sample to do so. 
   Public Overrides ReadOnly Property CanDelete() As Boolean 
      Get 
         Return False 
      End Get 
   End Property 

   Protected Overrides Function ExecuteDelete(keys As IDictionary, values As IDictionary) As Integer 
      Throw New NotSupportedException()
   End Function 'ExecuteDelete

   ' The CsvDataSourceView does not currently 
   ' permit insertion of a new record. You can 
   ' modify or extend this sample to do so. 
   Public Overrides ReadOnly Property CanInsert() As Boolean 
      Get 
         Return False 
      End Get 
   End Property 

   Protected Overrides Function ExecuteInsert(values As IDictionary) As Integer 
      Throw New NotSupportedException()
   End Function 'ExecuteInsert

   ' The CsvDataSourceView does not currently 
   ' permit update operations. You can modify or 
   ' extend this sample to do so. 
   Public Overrides ReadOnly Property CanUpdate() As Boolean 
      Get 
         Return False 
      End Get 
   End Property 

   Protected Overrides Function ExecuteUpdate(keys As IDictionary, _
                                              values As IDictionary, _
                                              oldValues As IDictionary) As Integer 
      Throw New NotSupportedException()
   End Function 'ExecuteUpdate

End Class 'CsvDataSourceView

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.