This documentation is archived and is not being maintained.

DataSourceView.ExecuteSelect Method

Gets a list of data from the underlying data storage.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

Protected Friend MustOverride Function ExecuteSelect ( _
	arguments As DataSourceSelectArguments _
) As IEnumerable
Dim arguments As DataSourceSelectArguments 
Dim returnValue As IEnumerable 

returnValue = Me.ExecuteSelect(arguments)


Type: System.Web.UI.DataSourceSelectArguments

A DataSourceSelectArguments that is used to request operations on the data beyond basic data retrieval.

Return Value

Type: System.Collections.IEnumerable
An IEnumerable list of data from the underlying data storage.

The ExecuteSelect method is called to retrieve data from the underlying data store and return it as an IEnumerable object. All data source controls support data retrieval from their underlying data storage, even if other operations such as insertion and sorting are not supported. Because a data-bound control can request a list of data at any time as a result of a DataSourceChanged event or a DataBind method call, the data retrieval must be performed on demand.

The following code example demonstrates how to override the ExecuteSelect method in a class that extends the DataSourceView class. The CsvDataSourceView opens a comma-separated value (.csv) file, parses it line by line, and creates a DataTable object and a DataView object to hold the data in memory. Finally, a sort expression is applied if one is supplied by the DataSourceSelectArguments object, and the DataView object is returned as an IEnumerable instance. This code example is part of a larger example provided for the DataSourceView class.

' 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)

         ' 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"))
         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
            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 

      End Try

      Dim dataView As New DataView(data)
      If Not selectArgs.SortExpression Is String.Empty Then
          dataView.Sort = selectArgs.SortExpression
      End If
      dataList = dataView
      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
      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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0