DataSourceView.ExecuteSelect Method (DataSourceSelectArguments)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets a list of data from the underlying data storage.

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

protected internal abstract IEnumerable ExecuteSelect(
	DataSourceSelectArguments 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 override IEnumerable ExecuteSelect(DataSourceSelectArguments selectArgs) {
    IEnumerable dataList = null;
    // Open the .csv file.
    if (File.Exists(this.SourceFile)) {
        DataTable data = new DataTable();

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(this.SourceFile)) {
            // Parse the line
            string s = "";
            string[] dataValues;
            DataColumn col;

            // Do the following to add schema.
            dataValues = sr.ReadLine().Split(',');
            // For each token in the comma-delimited string, add a column
            // to the DataTable schema.
            foreach (string token in dataValues) {
                col = new DataColumn(token,typeof(string));

            // Do not add the first row as data if the CSV file includes column names.
            if (! IncludesColumnNames)
                data.Rows.Add(CopyRowData(dataValues, data.NewRow()));

            // Do the following to add data.
            while ((s = sr.ReadLine()) != null) {
                dataValues = s.Split(',');
                data.Rows.Add(CopyRowData(dataValues, data.NewRow()));
        DataView dataView = new DataView(data);
        if (selectArgs.SortExpression != String.Empty) {
            dataView.Sort = selectArgs.SortExpression;
        dataList = dataView;
    else {
        throw new System.Configuration.ConfigurationErrorsException("File not found, " + this.SourceFile);

    if (null == dataList) {
        throw new InvalidOperationException("No data loaded from data source.");

    return dataList;

private DataRow CopyRowData(string[] source, DataRow target) {
    try {
        for (int i = 0;i < source.Length;i++) {
            target[i] = source[i];
    catch (System.IndexOutOfRangeException) {
        // There are more columns in this row than
        // the original schema allows.  Stop copying
        // and return the DataRow.
        return target;
    return target;

.NET Framework
Available since 2.0
Return to top