SqlDataSource.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ FXUpdate3074 / FXUpdate3074 / 1.1 / DEVDIV / depot / DevDiv / releases / whidbey / QFE / ndp / fx / src / xsp / System / Web / UI / WebControls / SqlDataSource.cs / 3 / SqlDataSource.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Web.UI.WebControls { 
 
    using System;
    using System.Web.Caching; 
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Common; 
    using System.Data.SqlClient;
    using System.Drawing; 
    using System.Drawing.Design; 
    using System.Globalization;
    using System.Security.Permissions; 
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls; 
    using System.IO;
    using System.Web.Util; 
 
    using ConflictOptions = System.Web.UI.ConflictOptions;
 

    /// 
    /// This class represents a datasource that uses an ADO.net connection to get
    /// its data. 
    /// ADO.net's provider factory model is used to support all managed providers
    /// registered in machine.config. 
    ///  
    [
    DefaultEvent("Selecting"), 
    DefaultProperty("SelectQuery"),
    Designer("System.Web.UI.Design.WebControls.SqlDataSourceDesigner, " + AssemblyRef.SystemDesign),
    ParseChildren(true),
    PersistChildren(false), 
    ToolboxBitmap(typeof(SqlDataSource)),
    WebSysDescription(SR.SqlDataSource_Description), 
    WebSysDisplayName(SR.SqlDataSource_DisplayName) 
    ]
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)] 
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public class SqlDataSource : DataSourceControl {

        private const string DefaultProviderName = "System.Data.SqlClient"; 
        private const string DefaultViewName = "DefaultView";
 
        private DataSourceCache _cache; 
        private string _cachedSelectCommand;
        private string _connectionString; 
        private SqlDataSourceMode _dataSourceMode = SqlDataSourceMode.DataSet;
        private string _providerName;
        private DbProviderFactory _providerFactory;
        private SqlDataSourceView _view; 
        private ICollection _viewNames;
 
 
        /// 
        /// Creates a new instance of SqlDataSource. 
        /// 
        public SqlDataSource() {
        }
 
        /// 
        /// Creates a new instance of SqlDataSource with a specified connection string and select command. 
        ///  
        public SqlDataSource(string connectionString, string selectCommand) {
            _connectionString = connectionString; 

            // Store the select command until the default view is created
            _cachedSelectCommand = selectCommand;
        } 

        ///  
        /// Creates a new instance of SqlDataSource with a specified provider name, connection string, and select command. 
        /// 
        public SqlDataSource(string providerName, string connectionString, string selectCommand) : this(connectionString, selectCommand) { 
            _providerName = providerName;
        }

 
        /// 
        /// Specifies the cache settings for this data source. For the cache to 
        /// work, the DataSourceMode must be set to DataSet. 
        /// 
        internal virtual DataSourceCache Cache { 
            get {
                if (_cache == null) {
                    _cache = new SqlDataSourceCache();
                } 
                return _cache;
            } 
        } 

        ///  
        /// The duration, in seconds, of the expiration. The expiration policy is specified by the CacheExpirationPolicy property.
        /// 
        [
        DefaultValue(DataSourceCache.Infinite), 
        TypeConverterAttribute(typeof(DataSourceCacheDurationConverter)),
        WebCategory("Cache"), 
        WebSysDescription(SR.DataSourceCache_Duration), 
        ]
        public virtual int CacheDuration { 
            get {
                return Cache.Duration;
            }
            set { 
                Cache.Duration = value;
            } 
        } 

        ///  
        /// The expiration policy of the cache. The duration for the expiration is specified by the CacheDuration property.
        /// 
        [
        DefaultValue(DataSourceCacheExpiry.Absolute), 
        WebCategory("Cache"),
        WebSysDescription(SR.DataSourceCache_ExpirationPolicy), 
        ] 
        public virtual DataSourceCacheExpiry CacheExpirationPolicy {
            get { 
                return Cache.ExpirationPolicy;
            }
            set {
                Cache.ExpirationPolicy = value; 
            }
        } 
 
        /// 
        /// Indicates an arbitrary cache key to make this cache entry depend on. This allows 
        /// the user to further customize when this cache entry will expire.
        /// 
        [
        DefaultValue(""), 
        WebCategory("Cache"),
        WebSysDescription(SR.DataSourceCache_KeyDependency), 
        ] 
        public virtual string CacheKeyDependency {
            get { 
                return Cache.KeyDependency;
            }
            set {
                Cache.KeyDependency = value; 
            }
        } 
 
        /// 
        /// Indicates whether the Select operation will be cancelled if the value of any of the SelectParameters is null. 
        /// 
        [
        DefaultValue(true),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_CancelSelectOnNullParameter),
        ] 
        public virtual bool CancelSelectOnNullParameter { 
            get {
                return GetView().CancelSelectOnNullParameter; 
            }
            set {
                GetView().CancelSelectOnNullParameter = value;
            } 
        }
 
        ///  
        /// Whether commands pass old values in the parameter collection.
        ///  
        [
        DefaultValue(ConflictOptions.OverwriteChanges),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_ConflictDetection), 
        ]
        public ConflictOptions ConflictDetection { 
            get { 
                return GetView().ConflictDetection;
            } 
            set {
                GetView().ConflictDetection = value;
            }
        } 

        ///  
        /// Gets/sets the connection string for the control. This property is not stored in ViewState. 
        /// 
        [ 
        DefaultValue(""),
        Editor("System.Web.UI.Design.WebControls.SqlDataSourceConnectionStringEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_ConnectionString), 
        ]
        public virtual string ConnectionString { 
            get { 
                return (_connectionString == null ? String.Empty : _connectionString);
            } 
            set {
                if (ConnectionString != value) {
                    _connectionString = value;
                    RaiseDataSourceChangedEvent(EventArgs.Empty); 
                }
            } 
        } 

        ///  
        /// Gets/sets the data source mode for the control.
        /// Only certain operations are supported depending on the data mode.
        /// 
        [ 
        DefaultValue(SqlDataSourceMode.DataSet),
        WebCategory("Behavior"), 
        WebSysDescription(SR.SqlDataSource_DataSourceMode), 
        ]
        public SqlDataSourceMode DataSourceMode { 
            get {
                return _dataSourceMode;
            }
            set { 
                if (value < SqlDataSourceMode.DataReader || value > SqlDataSourceMode.DataSet) {
                    throw new ArgumentOutOfRangeException(SR.GetString(SR.SqlDataSource_InvalidMode, ID)); 
                } 

                if (DataSourceMode != value) { 
                    _dataSourceMode = value;
                    RaiseDataSourceChangedEvent(EventArgs.Empty);
                }
            } 
        }
 
 
        /// 
        /// The command to execute when Delete() is called on the SqlDataSourceView. 
        /// 
        [
        DefaultValue(""),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_DeleteCommand),
        ] 
        public string DeleteCommand { 
            get {
                return GetView().DeleteCommand; 
            }
            set {
                GetView().DeleteCommand = value;
            } 
        }
 
        ///  
        /// The type of the delete command (command text or stored procedure).
        ///  
        [
        DefaultValue(SqlDataSourceCommandType.Text),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_DeleteCommandType), 
        ]
        public SqlDataSourceCommandType DeleteCommandType { 
            get { 
                return GetView().DeleteCommandType;
            } 
            set {
                GetView().DeleteCommandType = value;
            }
        } 

 
        ///  
        /// Collection of parameters used in Delete().
        ///  
        [
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        MergableProperty(false), 
        PersistenceMode(PersistenceMode.InnerProperty),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_DeleteParameters), 
        ]
        public ParameterCollection DeleteParameters { 
            get {
                return GetView().DeleteParameters;
            }
        } 

 
        ///  
        /// Whether caching is enabled for this data source.
        ///  
        [
        DefaultValue(false),
        WebCategory("Cache"),
        WebSysDescription(SR.DataSourceCache_Enabled), 
        ]
        public virtual bool EnableCaching { 
            get { 
                return Cache.Enabled;
            } 
            set {
                Cache.Enabled = value;
            }
        } 

 
        ///  
        /// The filter to apply when Select() is called on the SqlDataSourceView.
        ///  
        [
        DefaultValue(""),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_FilterExpression), 
        ]
        public string FilterExpression { 
            get { 
                return GetView().FilterExpression;
            } 
            set {
                GetView().FilterExpression = value;
            }
        } 

 
        ///  
        /// Collection of parameters used in the FilterExpression property.
        ///  
        [
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        MergableProperty(false), 
        PersistenceMode(PersistenceMode.InnerProperty),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_FilterParameters), 
        ]
        public ParameterCollection FilterParameters { 
            get {
                return GetView().FilterParameters;
            }
        } 

 
        ///  
        /// The command to execute when Insert() is called on the SqlDataSourceView.
        ///  
        [
        DefaultValue(""),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_InsertCommand), 
        ]
        public string InsertCommand { 
            get { 
                return GetView().InsertCommand;
            } 
            set {
                GetView().InsertCommand = value;
            }
        } 

        ///  
        /// The type of the insert command (command text or stored procedure). 
        /// 
        [ 
        DefaultValue(SqlDataSourceCommandType.Text),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_InsertCommandType),
        ] 
        public SqlDataSourceCommandType InsertCommandType {
            get { 
                return GetView().InsertCommandType; 
            }
            set { 
                GetView().InsertCommandType = value;
            }
        }
 

        ///  
        /// Collection of values used in Insert(). 
        /// 
        [ 
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        MergableProperty(false),
        PersistenceMode(PersistenceMode.InnerProperty), 
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_InsertParameters), 
        ] 
        public ParameterCollection InsertParameters {
            get { 
                return GetView().InsertParameters;
            }
        }
 
        /// 
        /// The format string applied to the names of the old values parameters 
        ///  
        [
        DefaultValue("{0}"), 
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_OldValuesParameterFormatString),
        ]
        public string OldValuesParameterFormatString { 
            get {
                return GetView().OldValuesParameterFormatString; 
            } 
            set {
                GetView().OldValuesParameterFormatString = value; 
            }
        }

 
        /// 
        /// Gets/sets the ADO.net managed provider name. 
        ///  
        [
        DefaultValue(""), 
        TypeConverter("System.Web.UI.Design.WebControls.DataProviderNameConverter, " + AssemblyRef.SystemDesign),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_ProviderName),
        ] 
        public virtual string ProviderName {
            get { 
                return (_providerName == null ? String.Empty : _providerName); 
            }
            set { 
                if (ProviderName != value) {
                    _providerFactory = null;
                    _providerName = value;
                    RaiseDataSourceChangedEvent(EventArgs.Empty); 
                }
            } 
        } 

 
        /// 
        /// The command to execute when Select() is called on the SqlDataSourceView.
        /// 
        [ 
        DefaultValue(""),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_SelectCommand), 
        ]
        public string SelectCommand { 
            get {
                return GetView().SelectCommand;
            }
            set { 
                GetView().SelectCommand = value;
            } 
        } 

        ///  
        /// The type of the select command (command text or stored procedure).
        /// 
        [
        DefaultValue(SqlDataSourceCommandType.Text), 
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_SelectCommandType), 
        ] 
        public SqlDataSourceCommandType SelectCommandType {
            get { 
                return GetView().SelectCommandType;
            }
            set {
                GetView().SelectCommandType = value; 
            }
        } 
 

        ///  
        /// The command to execute when Select is called on the SqlDataSourceView, requesting the total number of rows.
        /// 
        /* Commented out until we add paging support back into SqlDataSource
        [ 
        DefaultValue(""),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_SelectCountCommand), 
        ]
        public string SelectCountCommand { 
            get {
                return GetView().SelectCountCommand;
            }
            set { 
                GetView().SelectCountCommand = value;
            } 
        }*/ 

 
        /// 
        /// Collection of parameters used in Select().
        /// 
        [ 
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)), 
        MergableProperty(false), 
        PersistenceMode(PersistenceMode.InnerProperty),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_SelectParameters),
        ]
        public ParameterCollection SelectParameters {
            get { 
                return GetView().SelectParameters;
            } 
        } 

 
        /// 
        /// The name of the parameter in the SelectCommand that specifies the
        /// sort expression. This parameter's value will be automatically set
        /// at runtime with the appropriate sort expression. This is only 
        /// supported for stored procedure commands.
        ///  
        [ 
        DefaultValue(""),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_SortParameterName),
        ]
        public string SortParameterName {
            get { 
                return GetView().SortParameterName;
            } 
            set { 
                GetView().SortParameterName = value;
            } 
        }

        /// 
        /// Gets the SQL data source cache object. 
        /// 
        private SqlDataSourceCache SqlDataSourceCache { 
            get { 
                SqlDataSourceCache sqlCache = Cache as SqlDataSourceCache;
                if (sqlCache == null) { 
                    throw new NotSupportedException(SR.GetString(SR.SqlDataSource_SqlCacheDependencyNotSupported, ID));
                }
                return sqlCache;
            } 
        }
 
        ///  
        /// A semi-colon delimited string indicating which databases to use for the dependency in the format "database1:table1;database2:table2".
        ///  
        [
        DefaultValue(""),
        WebCategory("Cache"),
        WebSysDescription(SR.SqlDataSourceCache_SqlCacheDependency), 
        ]
        public virtual string SqlCacheDependency { 
            get { 
                return SqlDataSourceCache.SqlCacheDependency;
 
            }
            set {
                SqlDataSourceCache.SqlCacheDependency = value;
            } 
        }
 
 
        /// 
        /// The command to execute when Update() is called on the SqlDataSourceView. 
        /// 
        [
        DefaultValue(""),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_UpdateCommand),
        ] 
        public string UpdateCommand { 
            get {
                return GetView().UpdateCommand; 
            }
            set {
                GetView().UpdateCommand = value;
            } 
        }
 
        ///  
        /// The type of the update command (command text or stored procedure).
        ///  
        [
        DefaultValue(SqlDataSourceCommandType.Text),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_UpdateCommandType), 
        ]
        public SqlDataSourceCommandType UpdateCommandType { 
            get { 
                return GetView().UpdateCommandType;
            } 
            set {
                GetView().UpdateCommandType = value;
            }
        } 

 
        ///  
        /// Collection of parameters used in Update().
        ///  
        [
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        MergableProperty(false), 
        PersistenceMode(PersistenceMode.InnerProperty),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_UpdateParameters), 
        ]
        public ParameterCollection UpdateParameters { 
            get {
                return GetView().UpdateParameters;
            }
        } 

 
 
        /// 
        /// This event is raised after the Delete operation has completed. 
        /// Handle this event if you need to examine the values of output parameters.
        /// 
        [
        WebCategory("Data"), 
        WebSysDescription(SR.DataSource_Deleted),
        ] 
        public event SqlDataSourceStatusEventHandler Deleted { 
            add {
                GetView().Deleted += value; 
            }
            remove {
                GetView().Deleted -= value;
            } 
        }
 
 
        /// 
        /// This event is raised before the Delete operation has been executed. 
        /// Handle this event if you want to perform additional initialization operations
        /// that are specific to your application. You can also handle this event if you
        /// need to validate the values of parameters or change their values.
        /// When this event is raised, the database connection is not open yet, and you 
        /// can cancel the event by setting the Cancel property of the DataCommandEventArgs
        /// to true. 
        ///  
        [
        WebCategory("Data"), 
        WebSysDescription(SR.DataSource_Deleting),
        ]
        public event SqlDataSourceCommandEventHandler Deleting {
            add { 
                GetView().Deleting += value;
            } 
            remove { 
                GetView().Deleting -= value;
            } 
        }

        /// 
        /// This event is raised before the Filter operation takes place. 
        /// Handle this event if you want to perform validation operations on
        /// the parameter values. This event is only raised if the FilterExpression 
        /// is set. If the Cancel property of the event arguments is set to true, 
        /// the Select operation is aborted and the operation will return null.
        ///  
        [
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Filtering),
        ] 
        public event SqlDataSourceFilteringEventHandler Filtering {
            add { 
                GetView().Filtering += value; 
            }
            remove { 
                GetView().Filtering -= value;
            }
        }
 

        ///  
        /// This event is raised after the Insert operation has completed. 
        /// Handle this event if you need to examine the values of output parameters.
        ///  
        [
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Inserted),
        ] 
        public event SqlDataSourceStatusEventHandler Inserted {
            add { 
                GetView().Inserted += value; 
            }
            remove { 
                GetView().Inserted -= value;
            }
        }
 

        ///  
        /// This event is raised before the Insert operation has been executed. 
        /// Handle this event if you want to perform additional initialization operations
        /// that are specific to your application. You can also handle this event if you 
        /// need to validate the values of parameters or change their values.
        /// When this event is raised, the database connection is not open yet, and you
        /// can cancel the event by setting the Cancel property of the DataCommandEventArgs
        /// to true. 
        /// 
        [ 
        WebCategory("Data"), 
        WebSysDescription(SR.DataSource_Inserting),
        ] 
        public event SqlDataSourceCommandEventHandler Inserting {
            add {
                GetView().Inserting += value;
            } 
            remove {
                GetView().Inserting -= value; 
            } 
        }
 

        /// 
        /// This event is raised after the Select operation has completed.
        /// Handle this event if you need to examine the values of output parameters. 
        /// 
        [ 
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_Selected),
        ] 
        public event SqlDataSourceStatusEventHandler Selected {
            add {
                GetView().Selected += value;
            } 
            remove {
                GetView().Selected -= value; 
            } 
        }
 

        /// 
        /// This event is raised before the Select operation has been executed.
        /// Handle this event if you want to perform additional initialization operations 
        /// that are specific to your application. You can also handle this event if you
        /// need to validate the values of parameters or change their values. 
        /// When this event is raised, the database connection is not open yet, and you 
        /// can cancel the event by setting the Cancel property of the DataCommandEventArgs
        /// to true. 
        /// 
        [
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_Selecting), 
        ]
        public event SqlDataSourceSelectingEventHandler Selecting { 
            add { 
                GetView().Selecting += value;
            } 
            remove {
                GetView().Selecting -= value;
            }
        } 

 
        ///  
        /// This event is raised after the Update operation has completed.
        /// Handle this event if you need to examine the values of output parameters. 
        /// 
        [
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Updated), 
        ]
        public event SqlDataSourceStatusEventHandler Updated { 
            add { 
                GetView().Updated += value;
            } 
            remove {
                GetView().Updated -= value;
            }
        } 

 
        ///  
        /// This event is raised before the Update operation has been executed.
        /// Handle this event if you want to perform additional initialization operations 
        /// that are specific to your application. You can also handle this event if you
        /// need to validate the values of parameters or change their values.
        /// When this event is raised, the database connection is not open yet, and you
        /// can cancel the event by setting the Cancel property of the DataCommandEventArgs 
        /// to true.
        ///  
        [ 
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Updating), 
        ]
        public event SqlDataSourceCommandEventHandler Updating {
            add {
                GetView().Updating += value; 
            }
            remove { 
                GetView().Updating -= value; 
            }
        } 


        /// 
        /// Creates a unique cache key for this data source's data. 
        /// 
        internal string CreateCacheKey(int startRowIndex, int maximumRows) { 
            StringBuilder sb = CreateRawCacheKey(); 

            sb.Append(startRowIndex.ToString(CultureInfo.InvariantCulture)); 
            sb.Append(':');
            sb.Append(maximumRows.ToString(CultureInfo.InvariantCulture));

            return sb.ToString(); 
        }
 
        ///  
        /// Creates a DbConnection to the database based on the ProviderName.
        ///  
        internal DbConnection CreateConnection(string connectionString) {
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbConnection connection = factory.CreateConnection();
            connection.ConnectionString = connectionString; 
            return connection;
        } 
 
        /// 
        /// Creates a DbCommand based on the ProviderName. 
        /// 
        internal DbCommand CreateCommand(string commandText, DbConnection connection) {
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbCommand command = factory.CreateCommand(); 
            command.CommandText = commandText;
            command.Connection = connection; 
            return command; 
        }
 
        /// 
        /// Creates an DbDataAdapter based on the ProviderName.
        /// 
        internal DbDataAdapter CreateDataAdapter(DbCommand command) { 
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbDataAdapter dataAdapter = factory.CreateDataAdapter(); 
            dataAdapter.SelectCommand = command; 
            return dataAdapter;
        } 

        /// 
        /// Creates a SqlDataSourceView. Derived classes should override this if they need to return
        /// custom view types. 
        /// 
        protected virtual SqlDataSourceView CreateDataSourceView(string viewName) { 
            return new SqlDataSourceView(this, viewName, Context); 
        }
 
        /// 
        /// Creates the cache key for the master (parent) cache entry, which holds the total row count.
        /// 
        internal string CreateMasterCacheKey() { 
            return CreateRawCacheKey().ToString();
        } 
 
        /// 
        /// Creates an IDataParameter based on the ProviderName. 
        /// 
        internal DbParameter CreateParameter(string parameterName, object parameterValue) {
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbParameter parameter = factory.CreateParameter(); 
            parameter.ParameterName = parameterName;
            parameter.Value = parameterValue; 
            return parameter; 
        }
 
        /// 
        /// Returns the string for the raw master cache key.
        /// 
        private StringBuilder CreateRawCacheKey() { 
            // Note: The cache key will contain information such as server names and
            // passwords, however it will be stored in the internal cache, which is 
            // not accessible to page developers, so it is secure. 
            StringBuilder sb = new StringBuilder(CacheInternal.PrefixDataSourceControl, 1024);
            sb.Append(GetType().GetHashCode().ToString(CultureInfo.InvariantCulture)); 

            sb.Append(CacheDuration.ToString(CultureInfo.InvariantCulture));
            sb.Append(':');
            sb.Append(((int)CacheExpirationPolicy).ToString(CultureInfo.InvariantCulture)); 

            SqlDataSourceCache sqlCache = Cache as SqlDataSourceCache; 
            if (sqlCache != null) { 
                sb.Append(":");
                sb.Append(sqlCache.SqlCacheDependency); 
            }

            sb.Append(":");
            sb.Append(ConnectionString); 
            sb.Append(":");
            sb.Append(SelectCommand); 
            //sb.Append(SelectCountCommand); 

            // Append parameter names and values 
            if (SelectParameters.Count > 0) {
                sb.Append("?");
                IDictionary parameters = SelectParameters.GetValues(Context, this);
                foreach (DictionaryEntry entry in parameters) { 
                    sb.Append(entry.Key.ToString());
                    if ((entry.Value != null) && (entry.Value != DBNull.Value)) { 
                        sb.Append("="); 
                        sb.Append(entry.Value.ToString());
                    } 
                    else {
                        if (entry.Value == DBNull.Value) {
                            sb.Append("(dbnull)");
                        } 
                        else {
                            sb.Append("(null)"); 
                        } 
                    }
                    sb.Append("&"); 
                }
            }
            return sb;
        } 

        ///  
        /// Deletes rows from the data source using the parameters specified in the DeleteParameters collection. 
        /// 
        public int Delete() { 
            return GetView().Delete(null, null);
        }

        ///  
        /// Gets the DbProviderFactory associated with the provider type specified in the ProviderName property.
        /// If no provider is specified, the System.Data.SqlClient factory is used. 
        ///  
        protected virtual DbProviderFactory GetDbProviderFactory() {
            string providerName = ProviderName; 

            // Default to SQL provider
            if (String.IsNullOrEmpty(providerName)) {
                // use DefaultProviderName instance 
                return SqlClientFactory.Instance;
            } 
            else { 
                return DbProviderFactories.GetFactory(providerName);
            } 
        }

        /// 
        /// Gets the DbProviderFactory and performs a security 

        private DbProviderFactory GetDbProviderFactorySecure() { 
            if (_providerFactory == null) { 
                _providerFactory = GetDbProviderFactory();
 
                Debug.Assert(_providerFactory != null);

                // Perform security
                if (!HttpRuntime.ProcessRequestInApplicationTrust && !HttpRuntime.HasDbPermission(_providerFactory)) { 
                    throw new HttpException(SR.GetString(SR.SqlDataSource_NoDbPermission, _providerFactory.GetType().Name, ID));
                } 
            } 
            return _providerFactory;
        } 

        /// 
        /// Dynamically creates the default (and only) SqlDataSourceView on demand.
        ///  
        private SqlDataSourceView GetView() {
            if (_view == null) { 
                _view = CreateDataSourceView(DefaultViewName); 
                if (_cachedSelectCommand != null) {
                    // If there was a cached select command from the constructor, set it 
                    _view.SelectCommand = _cachedSelectCommand;
                }

                if (IsTrackingViewState) { 
                    ((IStateManager)_view).TrackViewState();
                } 
            } 

            return _view; 
        }

        /// 
        /// Gets the view associated with this connection. 
        /// SqlDataSource only supports a single view, so the viewName parameter is ignored.
        ///  
        protected override DataSourceView GetView(string viewName) { 
            if (viewName == null || (viewName.Length != 0 && !String.Equals(viewName, DefaultViewName, StringComparison.OrdinalIgnoreCase))) {
                throw new ArgumentException(SR.GetString(SR.DataSource_InvalidViewName, ID, DefaultViewName), "viewName"); 
            }

            return GetView();
        } 

        ///  
        /// Returns an ICollection of the names of all the views. In this case there is only one view called "Table". 
        /// 
        protected override ICollection GetViewNames() { 
            if (_viewNames == null) {
                _viewNames = new string[1] { DefaultViewName };
            }
            return _viewNames; 
        }
 
        ///  
        /// Inserts a new row with names and values specified the InsertValues collection.
        ///  
        public int Insert() {
            return GetView().Insert(null);
        }
 
        /// 
        /// Invalidates a cache entry. 
        ///  
        internal void InvalidateCacheEntry() {
            string key = CreateMasterCacheKey(); 
            DataSourceCache cache = Cache;
            Debug.Assert(cache != null);
            cache.Invalidate(key);
        } 

        ///  
        /// Event handler for the Page's LoadComplete event. 
        /// Updates the parameters' values to possibly raise a DataSourceChanged event, causing bound controls to re-databind.
        ///  
        private void LoadCompleteEventHandler(object sender, EventArgs e) {
            SelectParameters.UpdateValues(Context, this);
            FilterParameters.UpdateValues(Context, this);
        } 

        ///  
        /// Loads data from the cache. 
        /// 
        internal object LoadDataFromCache(int startRowIndex, int maximumRows) { 
            string key = CreateCacheKey(startRowIndex, maximumRows);
            return Cache.LoadDataFromCache(key);
        }
 
        /// 
        /// Loads data from the cache. 
        ///  
        internal int LoadTotalRowCountFromCache() {
            string key = CreateMasterCacheKey(); 
            object data = Cache.LoadDataFromCache(key);
            if (data is int)
                return (int)data;
            return -1; 
        }
 
        ///  
        /// Loads view state.
        ///  
        protected override void LoadViewState(object savedState) {
            Pair myState = (Pair)savedState;

            if (savedState == null) { 
                base.LoadViewState(null);
            } 
            else { 
                base.LoadViewState(myState.First);
 
                if (myState.Second != null) {
                    ((IStateManager)GetView()).LoadViewState(myState.Second);
                }
            } 
        }
 
        ///  
        /// Adds LoadComplete event handler to the page.
        ///  
        protected internal override void OnInit(EventArgs e) {
            base.OnInit(e);

            if (Page != null) { 
                Page.LoadComplete += new EventHandler(LoadCompleteEventHandler);
            } 
        } 

        ///  
        /// Saves paged data to cache, creating a dependency on the updated row count
        /// 
        internal virtual void SaveDataToCache(int startRowIndex, int maximumRows, object data, CacheDependency dependency) {
            string key = CreateCacheKey(startRowIndex, maximumRows); 
            string parentKey = CreateMasterCacheKey();
            if (Cache.LoadDataFromCache(parentKey) == null) { 
                Cache.SaveDataToCache(parentKey, -1, dependency); 
            }
            CacheDependency cacheDependency = new CacheDependency(0, new string[0], new string[] {parentKey}); 
            Cache.SaveDataToCache(key, data, cacheDependency);
        }

        ///  
        /// Saves the total row count to cache.
        ///  
        /*internal virtual void SaveTotalRowCountToCache(int totalRowCount) { 
            string key = CreateMasterCacheKey();
            Cache.SaveDataToCache(key, totalRowCount); 
        }*/

        /// 
        /// Saves view state. 
        /// 
        protected override object SaveViewState() { 
            Pair myState = new Pair(); 

            myState.First = base.SaveViewState(); 

            if (_view != null) {
                myState.Second = ((IStateManager)_view).SaveViewState();
            } 

            if ((myState.First == null) && 
                (myState.Second == null)) { 
                return null;
            } 

            return myState;
        }
 
        /// 
        /// Returns all the rows of the datasource. 
        /// Parameters are taken from the Parameters property collection. 
        /// If SqlDataSourceMode is set to DataSet then a DataView is returned.
        /// If SqlDataSourceMode is set to DataReader then a DataReader is returned, and it must be closed when done. 
        /// 
        public IEnumerable Select(DataSourceSelectArguments arguments) {
            return GetView().Select(arguments);
        } 

        ///  
        /// Starts tracking of view state. 
        /// 
        protected override void TrackViewState() { 
            base.TrackViewState();

            if (_view != null) {
                ((IStateManager)_view).TrackViewState(); 
            }
        } 
 
        /// 
        /// Updates rows matching the parameters specified in the UpdateParameters collection with new values specified the UpdateValues collection. 
        /// 
        public int Update() {
            return GetView().Update(null, null, null);
        } 
    }
} 
 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Web.UI.WebControls { 
 
    using System;
    using System.Web.Caching; 
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Common; 
    using System.Data.SqlClient;
    using System.Drawing; 
    using System.Drawing.Design; 
    using System.Globalization;
    using System.Security.Permissions; 
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls; 
    using System.IO;
    using System.Web.Util; 
 
    using ConflictOptions = System.Web.UI.ConflictOptions;
 

    /// 
    /// This class represents a datasource that uses an ADO.net connection to get
    /// its data. 
    /// ADO.net's provider factory model is used to support all managed providers
    /// registered in machine.config. 
    ///  
    [
    DefaultEvent("Selecting"), 
    DefaultProperty("SelectQuery"),
    Designer("System.Web.UI.Design.WebControls.SqlDataSourceDesigner, " + AssemblyRef.SystemDesign),
    ParseChildren(true),
    PersistChildren(false), 
    ToolboxBitmap(typeof(SqlDataSource)),
    WebSysDescription(SR.SqlDataSource_Description), 
    WebSysDisplayName(SR.SqlDataSource_DisplayName) 
    ]
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)] 
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public class SqlDataSource : DataSourceControl {

        private const string DefaultProviderName = "System.Data.SqlClient"; 
        private const string DefaultViewName = "DefaultView";
 
        private DataSourceCache _cache; 
        private string _cachedSelectCommand;
        private string _connectionString; 
        private SqlDataSourceMode _dataSourceMode = SqlDataSourceMode.DataSet;
        private string _providerName;
        private DbProviderFactory _providerFactory;
        private SqlDataSourceView _view; 
        private ICollection _viewNames;
 
 
        /// 
        /// Creates a new instance of SqlDataSource. 
        /// 
        public SqlDataSource() {
        }
 
        /// 
        /// Creates a new instance of SqlDataSource with a specified connection string and select command. 
        ///  
        public SqlDataSource(string connectionString, string selectCommand) {
            _connectionString = connectionString; 

            // Store the select command until the default view is created
            _cachedSelectCommand = selectCommand;
        } 

        ///  
        /// Creates a new instance of SqlDataSource with a specified provider name, connection string, and select command. 
        /// 
        public SqlDataSource(string providerName, string connectionString, string selectCommand) : this(connectionString, selectCommand) { 
            _providerName = providerName;
        }

 
        /// 
        /// Specifies the cache settings for this data source. For the cache to 
        /// work, the DataSourceMode must be set to DataSet. 
        /// 
        internal virtual DataSourceCache Cache { 
            get {
                if (_cache == null) {
                    _cache = new SqlDataSourceCache();
                } 
                return _cache;
            } 
        } 

        ///  
        /// The duration, in seconds, of the expiration. The expiration policy is specified by the CacheExpirationPolicy property.
        /// 
        [
        DefaultValue(DataSourceCache.Infinite), 
        TypeConverterAttribute(typeof(DataSourceCacheDurationConverter)),
        WebCategory("Cache"), 
        WebSysDescription(SR.DataSourceCache_Duration), 
        ]
        public virtual int CacheDuration { 
            get {
                return Cache.Duration;
            }
            set { 
                Cache.Duration = value;
            } 
        } 

        ///  
        /// The expiration policy of the cache. The duration for the expiration is specified by the CacheDuration property.
        /// 
        [
        DefaultValue(DataSourceCacheExpiry.Absolute), 
        WebCategory("Cache"),
        WebSysDescription(SR.DataSourceCache_ExpirationPolicy), 
        ] 
        public virtual DataSourceCacheExpiry CacheExpirationPolicy {
            get { 
                return Cache.ExpirationPolicy;
            }
            set {
                Cache.ExpirationPolicy = value; 
            }
        } 
 
        /// 
        /// Indicates an arbitrary cache key to make this cache entry depend on. This allows 
        /// the user to further customize when this cache entry will expire.
        /// 
        [
        DefaultValue(""), 
        WebCategory("Cache"),
        WebSysDescription(SR.DataSourceCache_KeyDependency), 
        ] 
        public virtual string CacheKeyDependency {
            get { 
                return Cache.KeyDependency;
            }
            set {
                Cache.KeyDependency = value; 
            }
        } 
 
        /// 
        /// Indicates whether the Select operation will be cancelled if the value of any of the SelectParameters is null. 
        /// 
        [
        DefaultValue(true),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_CancelSelectOnNullParameter),
        ] 
        public virtual bool CancelSelectOnNullParameter { 
            get {
                return GetView().CancelSelectOnNullParameter; 
            }
            set {
                GetView().CancelSelectOnNullParameter = value;
            } 
        }
 
        ///  
        /// Whether commands pass old values in the parameter collection.
        ///  
        [
        DefaultValue(ConflictOptions.OverwriteChanges),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_ConflictDetection), 
        ]
        public ConflictOptions ConflictDetection { 
            get { 
                return GetView().ConflictDetection;
            } 
            set {
                GetView().ConflictDetection = value;
            }
        } 

        ///  
        /// Gets/sets the connection string for the control. This property is not stored in ViewState. 
        /// 
        [ 
        DefaultValue(""),
        Editor("System.Web.UI.Design.WebControls.SqlDataSourceConnectionStringEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_ConnectionString), 
        ]
        public virtual string ConnectionString { 
            get { 
                return (_connectionString == null ? String.Empty : _connectionString);
            } 
            set {
                if (ConnectionString != value) {
                    _connectionString = value;
                    RaiseDataSourceChangedEvent(EventArgs.Empty); 
                }
            } 
        } 

        ///  
        /// Gets/sets the data source mode for the control.
        /// Only certain operations are supported depending on the data mode.
        /// 
        [ 
        DefaultValue(SqlDataSourceMode.DataSet),
        WebCategory("Behavior"), 
        WebSysDescription(SR.SqlDataSource_DataSourceMode), 
        ]
        public SqlDataSourceMode DataSourceMode { 
            get {
                return _dataSourceMode;
            }
            set { 
                if (value < SqlDataSourceMode.DataReader || value > SqlDataSourceMode.DataSet) {
                    throw new ArgumentOutOfRangeException(SR.GetString(SR.SqlDataSource_InvalidMode, ID)); 
                } 

                if (DataSourceMode != value) { 
                    _dataSourceMode = value;
                    RaiseDataSourceChangedEvent(EventArgs.Empty);
                }
            } 
        }
 
 
        /// 
        /// The command to execute when Delete() is called on the SqlDataSourceView. 
        /// 
        [
        DefaultValue(""),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_DeleteCommand),
        ] 
        public string DeleteCommand { 
            get {
                return GetView().DeleteCommand; 
            }
            set {
                GetView().DeleteCommand = value;
            } 
        }
 
        ///  
        /// The type of the delete command (command text or stored procedure).
        ///  
        [
        DefaultValue(SqlDataSourceCommandType.Text),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_DeleteCommandType), 
        ]
        public SqlDataSourceCommandType DeleteCommandType { 
            get { 
                return GetView().DeleteCommandType;
            } 
            set {
                GetView().DeleteCommandType = value;
            }
        } 

 
        ///  
        /// Collection of parameters used in Delete().
        ///  
        [
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        MergableProperty(false), 
        PersistenceMode(PersistenceMode.InnerProperty),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_DeleteParameters), 
        ]
        public ParameterCollection DeleteParameters { 
            get {
                return GetView().DeleteParameters;
            }
        } 

 
        ///  
        /// Whether caching is enabled for this data source.
        ///  
        [
        DefaultValue(false),
        WebCategory("Cache"),
        WebSysDescription(SR.DataSourceCache_Enabled), 
        ]
        public virtual bool EnableCaching { 
            get { 
                return Cache.Enabled;
            } 
            set {
                Cache.Enabled = value;
            }
        } 

 
        ///  
        /// The filter to apply when Select() is called on the SqlDataSourceView.
        ///  
        [
        DefaultValue(""),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_FilterExpression), 
        ]
        public string FilterExpression { 
            get { 
                return GetView().FilterExpression;
            } 
            set {
                GetView().FilterExpression = value;
            }
        } 

 
        ///  
        /// Collection of parameters used in the FilterExpression property.
        ///  
        [
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        MergableProperty(false), 
        PersistenceMode(PersistenceMode.InnerProperty),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_FilterParameters), 
        ]
        public ParameterCollection FilterParameters { 
            get {
                return GetView().FilterParameters;
            }
        } 

 
        ///  
        /// The command to execute when Insert() is called on the SqlDataSourceView.
        ///  
        [
        DefaultValue(""),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_InsertCommand), 
        ]
        public string InsertCommand { 
            get { 
                return GetView().InsertCommand;
            } 
            set {
                GetView().InsertCommand = value;
            }
        } 

        ///  
        /// The type of the insert command (command text or stored procedure). 
        /// 
        [ 
        DefaultValue(SqlDataSourceCommandType.Text),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_InsertCommandType),
        ] 
        public SqlDataSourceCommandType InsertCommandType {
            get { 
                return GetView().InsertCommandType; 
            }
            set { 
                GetView().InsertCommandType = value;
            }
        }
 

        ///  
        /// Collection of values used in Insert(). 
        /// 
        [ 
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        MergableProperty(false),
        PersistenceMode(PersistenceMode.InnerProperty), 
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_InsertParameters), 
        ] 
        public ParameterCollection InsertParameters {
            get { 
                return GetView().InsertParameters;
            }
        }
 
        /// 
        /// The format string applied to the names of the old values parameters 
        ///  
        [
        DefaultValue("{0}"), 
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_OldValuesParameterFormatString),
        ]
        public string OldValuesParameterFormatString { 
            get {
                return GetView().OldValuesParameterFormatString; 
            } 
            set {
                GetView().OldValuesParameterFormatString = value; 
            }
        }

 
        /// 
        /// Gets/sets the ADO.net managed provider name. 
        ///  
        [
        DefaultValue(""), 
        TypeConverter("System.Web.UI.Design.WebControls.DataProviderNameConverter, " + AssemblyRef.SystemDesign),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_ProviderName),
        ] 
        public virtual string ProviderName {
            get { 
                return (_providerName == null ? String.Empty : _providerName); 
            }
            set { 
                if (ProviderName != value) {
                    _providerFactory = null;
                    _providerName = value;
                    RaiseDataSourceChangedEvent(EventArgs.Empty); 
                }
            } 
        } 

 
        /// 
        /// The command to execute when Select() is called on the SqlDataSourceView.
        /// 
        [ 
        DefaultValue(""),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_SelectCommand), 
        ]
        public string SelectCommand { 
            get {
                return GetView().SelectCommand;
            }
            set { 
                GetView().SelectCommand = value;
            } 
        } 

        ///  
        /// The type of the select command (command text or stored procedure).
        /// 
        [
        DefaultValue(SqlDataSourceCommandType.Text), 
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_SelectCommandType), 
        ] 
        public SqlDataSourceCommandType SelectCommandType {
            get { 
                return GetView().SelectCommandType;
            }
            set {
                GetView().SelectCommandType = value; 
            }
        } 
 

        ///  
        /// The command to execute when Select is called on the SqlDataSourceView, requesting the total number of rows.
        /// 
        /* Commented out until we add paging support back into SqlDataSource
        [ 
        DefaultValue(""),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_SelectCountCommand), 
        ]
        public string SelectCountCommand { 
            get {
                return GetView().SelectCountCommand;
            }
            set { 
                GetView().SelectCountCommand = value;
            } 
        }*/ 

 
        /// 
        /// Collection of parameters used in Select().
        /// 
        [ 
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)), 
        MergableProperty(false), 
        PersistenceMode(PersistenceMode.InnerProperty),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_SelectParameters),
        ]
        public ParameterCollection SelectParameters {
            get { 
                return GetView().SelectParameters;
            } 
        } 

 
        /// 
        /// The name of the parameter in the SelectCommand that specifies the
        /// sort expression. This parameter's value will be automatically set
        /// at runtime with the appropriate sort expression. This is only 
        /// supported for stored procedure commands.
        ///  
        [ 
        DefaultValue(""),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_SortParameterName),
        ]
        public string SortParameterName {
            get { 
                return GetView().SortParameterName;
            } 
            set { 
                GetView().SortParameterName = value;
            } 
        }

        /// 
        /// Gets the SQL data source cache object. 
        /// 
        private SqlDataSourceCache SqlDataSourceCache { 
            get { 
                SqlDataSourceCache sqlCache = Cache as SqlDataSourceCache;
                if (sqlCache == null) { 
                    throw new NotSupportedException(SR.GetString(SR.SqlDataSource_SqlCacheDependencyNotSupported, ID));
                }
                return sqlCache;
            } 
        }
 
        ///  
        /// A semi-colon delimited string indicating which databases to use for the dependency in the format "database1:table1;database2:table2".
        ///  
        [
        DefaultValue(""),
        WebCategory("Cache"),
        WebSysDescription(SR.SqlDataSourceCache_SqlCacheDependency), 
        ]
        public virtual string SqlCacheDependency { 
            get { 
                return SqlDataSourceCache.SqlCacheDependency;
 
            }
            set {
                SqlDataSourceCache.SqlCacheDependency = value;
            } 
        }
 
 
        /// 
        /// The command to execute when Update() is called on the SqlDataSourceView. 
        /// 
        [
        DefaultValue(""),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_UpdateCommand),
        ] 
        public string UpdateCommand { 
            get {
                return GetView().UpdateCommand; 
            }
            set {
                GetView().UpdateCommand = value;
            } 
        }
 
        ///  
        /// The type of the update command (command text or stored procedure).
        ///  
        [
        DefaultValue(SqlDataSourceCommandType.Text),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_UpdateCommandType), 
        ]
        public SqlDataSourceCommandType UpdateCommandType { 
            get { 
                return GetView().UpdateCommandType;
            } 
            set {
                GetView().UpdateCommandType = value;
            }
        } 

 
        ///  
        /// Collection of parameters used in Update().
        ///  
        [
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        MergableProperty(false), 
        PersistenceMode(PersistenceMode.InnerProperty),
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_UpdateParameters), 
        ]
        public ParameterCollection UpdateParameters { 
            get {
                return GetView().UpdateParameters;
            }
        } 

 
 
        /// 
        /// This event is raised after the Delete operation has completed. 
        /// Handle this event if you need to examine the values of output parameters.
        /// 
        [
        WebCategory("Data"), 
        WebSysDescription(SR.DataSource_Deleted),
        ] 
        public event SqlDataSourceStatusEventHandler Deleted { 
            add {
                GetView().Deleted += value; 
            }
            remove {
                GetView().Deleted -= value;
            } 
        }
 
 
        /// 
        /// This event is raised before the Delete operation has been executed. 
        /// Handle this event if you want to perform additional initialization operations
        /// that are specific to your application. You can also handle this event if you
        /// need to validate the values of parameters or change their values.
        /// When this event is raised, the database connection is not open yet, and you 
        /// can cancel the event by setting the Cancel property of the DataCommandEventArgs
        /// to true. 
        ///  
        [
        WebCategory("Data"), 
        WebSysDescription(SR.DataSource_Deleting),
        ]
        public event SqlDataSourceCommandEventHandler Deleting {
            add { 
                GetView().Deleting += value;
            } 
            remove { 
                GetView().Deleting -= value;
            } 
        }

        /// 
        /// This event is raised before the Filter operation takes place. 
        /// Handle this event if you want to perform validation operations on
        /// the parameter values. This event is only raised if the FilterExpression 
        /// is set. If the Cancel property of the event arguments is set to true, 
        /// the Select operation is aborted and the operation will return null.
        ///  
        [
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Filtering),
        ] 
        public event SqlDataSourceFilteringEventHandler Filtering {
            add { 
                GetView().Filtering += value; 
            }
            remove { 
                GetView().Filtering -= value;
            }
        }
 

        ///  
        /// This event is raised after the Insert operation has completed. 
        /// Handle this event if you need to examine the values of output parameters.
        ///  
        [
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Inserted),
        ] 
        public event SqlDataSourceStatusEventHandler Inserted {
            add { 
                GetView().Inserted += value; 
            }
            remove { 
                GetView().Inserted -= value;
            }
        }
 

        ///  
        /// This event is raised before the Insert operation has been executed. 
        /// Handle this event if you want to perform additional initialization operations
        /// that are specific to your application. You can also handle this event if you 
        /// need to validate the values of parameters or change their values.
        /// When this event is raised, the database connection is not open yet, and you
        /// can cancel the event by setting the Cancel property of the DataCommandEventArgs
        /// to true. 
        /// 
        [ 
        WebCategory("Data"), 
        WebSysDescription(SR.DataSource_Inserting),
        ] 
        public event SqlDataSourceCommandEventHandler Inserting {
            add {
                GetView().Inserting += value;
            } 
            remove {
                GetView().Inserting -= value; 
            } 
        }
 

        /// 
        /// This event is raised after the Select operation has completed.
        /// Handle this event if you need to examine the values of output parameters. 
        /// 
        [ 
        WebCategory("Data"), 
        WebSysDescription(SR.SqlDataSource_Selected),
        ] 
        public event SqlDataSourceStatusEventHandler Selected {
            add {
                GetView().Selected += value;
            } 
            remove {
                GetView().Selected -= value; 
            } 
        }
 

        /// 
        /// This event is raised before the Select operation has been executed.
        /// Handle this event if you want to perform additional initialization operations 
        /// that are specific to your application. You can also handle this event if you
        /// need to validate the values of parameters or change their values. 
        /// When this event is raised, the database connection is not open yet, and you 
        /// can cancel the event by setting the Cancel property of the DataCommandEventArgs
        /// to true. 
        /// 
        [
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_Selecting), 
        ]
        public event SqlDataSourceSelectingEventHandler Selecting { 
            add { 
                GetView().Selecting += value;
            } 
            remove {
                GetView().Selecting -= value;
            }
        } 

 
        ///  
        /// This event is raised after the Update operation has completed.
        /// Handle this event if you need to examine the values of output parameters. 
        /// 
        [
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Updated), 
        ]
        public event SqlDataSourceStatusEventHandler Updated { 
            add { 
                GetView().Updated += value;
            } 
            remove {
                GetView().Updated -= value;
            }
        } 

 
        ///  
        /// This event is raised before the Update operation has been executed.
        /// Handle this event if you want to perform additional initialization operations 
        /// that are specific to your application. You can also handle this event if you
        /// need to validate the values of parameters or change their values.
        /// When this event is raised, the database connection is not open yet, and you
        /// can cancel the event by setting the Cancel property of the DataCommandEventArgs 
        /// to true.
        ///  
        [ 
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Updating), 
        ]
        public event SqlDataSourceCommandEventHandler Updating {
            add {
                GetView().Updating += value; 
            }
            remove { 
                GetView().Updating -= value; 
            }
        } 


        /// 
        /// Creates a unique cache key for this data source's data. 
        /// 
        internal string CreateCacheKey(int startRowIndex, int maximumRows) { 
            StringBuilder sb = CreateRawCacheKey(); 

            sb.Append(startRowIndex.ToString(CultureInfo.InvariantCulture)); 
            sb.Append(':');
            sb.Append(maximumRows.ToString(CultureInfo.InvariantCulture));

            return sb.ToString(); 
        }
 
        ///  
        /// Creates a DbConnection to the database based on the ProviderName.
        ///  
        internal DbConnection CreateConnection(string connectionString) {
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbConnection connection = factory.CreateConnection();
            connection.ConnectionString = connectionString; 
            return connection;
        } 
 
        /// 
        /// Creates a DbCommand based on the ProviderName. 
        /// 
        internal DbCommand CreateCommand(string commandText, DbConnection connection) {
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbCommand command = factory.CreateCommand(); 
            command.CommandText = commandText;
            command.Connection = connection; 
            return command; 
        }
 
        /// 
        /// Creates an DbDataAdapter based on the ProviderName.
        /// 
        internal DbDataAdapter CreateDataAdapter(DbCommand command) { 
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbDataAdapter dataAdapter = factory.CreateDataAdapter(); 
            dataAdapter.SelectCommand = command; 
            return dataAdapter;
        } 

        /// 
        /// Creates a SqlDataSourceView. Derived classes should override this if they need to return
        /// custom view types. 
        /// 
        protected virtual SqlDataSourceView CreateDataSourceView(string viewName) { 
            return new SqlDataSourceView(this, viewName, Context); 
        }
 
        /// 
        /// Creates the cache key for the master (parent) cache entry, which holds the total row count.
        /// 
        internal string CreateMasterCacheKey() { 
            return CreateRawCacheKey().ToString();
        } 
 
        /// 
        /// Creates an IDataParameter based on the ProviderName. 
        /// 
        internal DbParameter CreateParameter(string parameterName, object parameterValue) {
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbParameter parameter = factory.CreateParameter(); 
            parameter.ParameterName = parameterName;
            parameter.Value = parameterValue; 
            return parameter; 
        }
 
        /// 
        /// Returns the string for the raw master cache key.
        /// 
        private StringBuilder CreateRawCacheKey() { 
            // Note: The cache key will contain information such as server names and
            // passwords, however it will be stored in the internal cache, which is 
            // not accessible to page developers, so it is secure. 
            StringBuilder sb = new StringBuilder(CacheInternal.PrefixDataSourceControl, 1024);
            sb.Append(GetType().GetHashCode().ToString(CultureInfo.InvariantCulture)); 

            sb.Append(CacheDuration.ToString(CultureInfo.InvariantCulture));
            sb.Append(':');
            sb.Append(((int)CacheExpirationPolicy).ToString(CultureInfo.InvariantCulture)); 

            SqlDataSourceCache sqlCache = Cache as SqlDataSourceCache; 
            if (sqlCache != null) { 
                sb.Append(":");
                sb.Append(sqlCache.SqlCacheDependency); 
            }

            sb.Append(":");
            sb.Append(ConnectionString); 
            sb.Append(":");
            sb.Append(SelectCommand); 
            //sb.Append(SelectCountCommand); 

            // Append parameter names and values 
            if (SelectParameters.Count > 0) {
                sb.Append("?");
                IDictionary parameters = SelectParameters.GetValues(Context, this);
                foreach (DictionaryEntry entry in parameters) { 
                    sb.Append(entry.Key.ToString());
                    if ((entry.Value != null) && (entry.Value != DBNull.Value)) { 
                        sb.Append("="); 
                        sb.Append(entry.Value.ToString());
                    } 
                    else {
                        if (entry.Value == DBNull.Value) {
                            sb.Append("(dbnull)");
                        } 
                        else {
                            sb.Append("(null)"); 
                        } 
                    }
                    sb.Append("&"); 
                }
            }
            return sb;
        } 

        ///  
        /// Deletes rows from the data source using the parameters specified in the DeleteParameters collection. 
        /// 
        public int Delete() { 
            return GetView().Delete(null, null);
        }

        ///  
        /// Gets the DbProviderFactory associated with the provider type specified in the ProviderName property.
        /// If no provider is specified, the System.Data.SqlClient factory is used. 
        ///  
        protected virtual DbProviderFactory GetDbProviderFactory() {
            string providerName = ProviderName; 

            // Default to SQL provider
            if (String.IsNullOrEmpty(providerName)) {
                // use DefaultProviderName instance 
                return SqlClientFactory.Instance;
            } 
            else { 
                return DbProviderFactories.GetFactory(providerName);
            } 
        }

        /// 
        /// Gets the DbProviderFactory and performs a security 

        private DbProviderFactory GetDbProviderFactorySecure() { 
            if (_providerFactory == null) { 
                _providerFactory = GetDbProviderFactory();
 
                Debug.Assert(_providerFactory != null);

                // Perform security
                if (!HttpRuntime.ProcessRequestInApplicationTrust && !HttpRuntime.HasDbPermission(_providerFactory)) { 
                    throw new HttpException(SR.GetString(SR.SqlDataSource_NoDbPermission, _providerFactory.GetType().Name, ID));
                } 
            } 
            return _providerFactory;
        } 

        /// 
        /// Dynamically creates the default (and only) SqlDataSourceView on demand.
        ///  
        private SqlDataSourceView GetView() {
            if (_view == null) { 
                _view = CreateDataSourceView(DefaultViewName); 
                if (_cachedSelectCommand != null) {
                    // If there was a cached select command from the constructor, set it 
                    _view.SelectCommand = _cachedSelectCommand;
                }

                if (IsTrackingViewState) { 
                    ((IStateManager)_view).TrackViewState();
                } 
            } 

            return _view; 
        }

        /// 
        /// Gets the view associated with this connection. 
        /// SqlDataSource only supports a single view, so the viewName parameter is ignored.
        ///  
        protected override DataSourceView GetView(string viewName) { 
            if (viewName == null || (viewName.Length != 0 && !String.Equals(viewName, DefaultViewName, StringComparison.OrdinalIgnoreCase))) {
                throw new ArgumentException(SR.GetString(SR.DataSource_InvalidViewName, ID, DefaultViewName), "viewName"); 
            }

            return GetView();
        } 

        ///  
        /// Returns an ICollection of the names of all the views. In this case there is only one view called "Table". 
        /// 
        protected override ICollection GetViewNames() { 
            if (_viewNames == null) {
                _viewNames = new string[1] { DefaultViewName };
            }
            return _viewNames; 
        }
 
        ///  
        /// Inserts a new row with names and values specified the InsertValues collection.
        ///  
        public int Insert() {
            return GetView().Insert(null);
        }
 
        /// 
        /// Invalidates a cache entry. 
        ///  
        internal void InvalidateCacheEntry() {
            string key = CreateMasterCacheKey(); 
            DataSourceCache cache = Cache;
            Debug.Assert(cache != null);
            cache.Invalidate(key);
        } 

        ///  
        /// Event handler for the Page's LoadComplete event. 
        /// Updates the parameters' values to possibly raise a DataSourceChanged event, causing bound controls to re-databind.
        ///  
        private void LoadCompleteEventHandler(object sender, EventArgs e) {
            SelectParameters.UpdateValues(Context, this);
            FilterParameters.UpdateValues(Context, this);
        } 

        ///  
        /// Loads data from the cache. 
        /// 
        internal object LoadDataFromCache(int startRowIndex, int maximumRows) { 
            string key = CreateCacheKey(startRowIndex, maximumRows);
            return Cache.LoadDataFromCache(key);
        }
 
        /// 
        /// Loads data from the cache. 
        ///  
        internal int LoadTotalRowCountFromCache() {
            string key = CreateMasterCacheKey(); 
            object data = Cache.LoadDataFromCache(key);
            if (data is int)
                return (int)data;
            return -1; 
        }
 
        ///  
        /// Loads view state.
        ///  
        protected override void LoadViewState(object savedState) {
            Pair myState = (Pair)savedState;

            if (savedState == null) { 
                base.LoadViewState(null);
            } 
            else { 
                base.LoadViewState(myState.First);
 
                if (myState.Second != null) {
                    ((IStateManager)GetView()).LoadViewState(myState.Second);
                }
            } 
        }
 
        ///  
        /// Adds LoadComplete event handler to the page.
        ///  
        protected internal override void OnInit(EventArgs e) {
            base.OnInit(e);

            if (Page != null) { 
                Page.LoadComplete += new EventHandler(LoadCompleteEventHandler);
            } 
        } 

        ///  
        /// Saves paged data to cache, creating a dependency on the updated row count
        /// 
        internal virtual void SaveDataToCache(int startRowIndex, int maximumRows, object data, CacheDependency dependency) {
            string key = CreateCacheKey(startRowIndex, maximumRows); 
            string parentKey = CreateMasterCacheKey();
            if (Cache.LoadDataFromCache(parentKey) == null) { 
                Cache.SaveDataToCache(parentKey, -1, dependency); 
            }
            CacheDependency cacheDependency = new CacheDependency(0, new string[0], new string[] {parentKey}); 
            Cache.SaveDataToCache(key, data, cacheDependency);
        }

        ///  
        /// Saves the total row count to cache.
        ///  
        /*internal virtual void SaveTotalRowCountToCache(int totalRowCount) { 
            string key = CreateMasterCacheKey();
            Cache.SaveDataToCache(key, totalRowCount); 
        }*/

        /// 
        /// Saves view state. 
        /// 
        protected override object SaveViewState() { 
            Pair myState = new Pair(); 

            myState.First = base.SaveViewState(); 

            if (_view != null) {
                myState.Second = ((IStateManager)_view).SaveViewState();
            } 

            if ((myState.First == null) && 
                (myState.Second == null)) { 
                return null;
            } 

            return myState;
        }
 
        /// 
        /// Returns all the rows of the datasource. 
        /// Parameters are taken from the Parameters property collection. 
        /// If SqlDataSourceMode is set to DataSet then a DataView is returned.
        /// If SqlDataSourceMode is set to DataReader then a DataReader is returned, and it must be closed when done. 
        /// 
        public IEnumerable Select(DataSourceSelectArguments arguments) {
            return GetView().Select(arguments);
        } 

        ///  
        /// Starts tracking of view state. 
        /// 
        protected override void TrackViewState() { 
            base.TrackViewState();

            if (_view != null) {
                ((IStateManager)_view).TrackViewState(); 
            }
        } 
 
        /// 
        /// Updates rows matching the parameters specified in the UpdateParameters collection with new values specified the UpdateValues collection. 
        /// 
        public int Update() {
            return GetView().Update(null, null, null);
        } 
    }
} 
 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK