File: SqlDataSource.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (1141 lines) | stat: -rw-r--r-- 39,682 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
//------------------------------------------------------------------------------
// <copyright file="SqlDataSource.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls {

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Diagnostics.CodeAnalysis;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Globalization;
    using System.Text;
    using System.Web;
    using System.Web.Caching;
    using System.Web.UI;
    using System.Web.Util;
    using ConflictOptions = System.Web.UI.ConflictOptions;


    /// <devdoc>
    /// 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.
    /// </devdoc>
    [
    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)
    ]
    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;


        /// <devdoc>
        /// Creates a new instance of SqlDataSource.
        /// </devdoc>
        public SqlDataSource() {
        }

        /// <devdoc>
        /// Creates a new instance of SqlDataSource with a specified connection string and select command.
        /// </devdoc>
        public SqlDataSource(string connectionString, string selectCommand) {
            _connectionString = connectionString;

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

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


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

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

        /// <devdoc>
        /// The expiration policy of the cache. The duration for the expiration is specified by the CacheDuration property.
        /// </devdoc>
        [
        DefaultValue(DataSourceCacheExpiry.Absolute),
        WebCategory("Cache"),
        WebSysDescription(SR.DataSourceCache_ExpirationPolicy),
        ]
        public virtual DataSourceCacheExpiry CacheExpirationPolicy {
            get {
                return Cache.ExpirationPolicy;
            }
            set {
                Cache.ExpirationPolicy = value;
            }
        }

        /// <devdoc>
        /// 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.
        /// </devdoc>
        [
        DefaultValue(""),
        WebCategory("Cache"),
        WebSysDescription(SR.DataSourceCache_KeyDependency),
        ]
        public virtual string CacheKeyDependency {
            get {
                return Cache.KeyDependency;
            }
            set {
                Cache.KeyDependency = value;
            }
        }

        /// <devdoc>
        /// Indicates whether the Select operation will be cancelled if the value of any of the SelectParameters is null.
        /// </devdoc>
        [
        DefaultValue(true),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_CancelSelectOnNullParameter),
        ]
        public virtual bool CancelSelectOnNullParameter {
            get {
                return GetView().CancelSelectOnNullParameter;
            }
            set {
                GetView().CancelSelectOnNullParameter = value;
            }
        }

        /// <devdoc>
        /// Whether commands pass old values in the parameter collection.
        /// </devdoc>
        [
        DefaultValue(ConflictOptions.OverwriteChanges),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_ConflictDetection),
        ]
        public ConflictOptions ConflictDetection {
            get {
                return GetView().ConflictDetection;
            }
            set {
                GetView().ConflictDetection = value;
            }
        }

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

        /// <devdoc>
        /// Gets/sets the data source mode for the control.
        /// Only certain operations are supported depending on the data mode.
        /// </devdoc>
        [
        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);
                }
            }
        }


        /// <devdoc>
        /// The command to execute when Delete() is called on the SqlDataSourceView.
        /// </devdoc>
        [
        DefaultValue(""),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_DeleteCommand),
        ]
        public string DeleteCommand {
            get {
                return GetView().DeleteCommand;
            }
            set {
                GetView().DeleteCommand = value;
            }
        }

        /// <devdoc>
        /// The type of the delete command (command text or stored procedure).
        /// </devdoc>
        [
        DefaultValue(SqlDataSourceCommandType.Text),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_DeleteCommandType),
        ]
        public SqlDataSourceCommandType DeleteCommandType {
            get {
                return GetView().DeleteCommandType;
            }
            set {
                GetView().DeleteCommandType = value;
            }
        }


        /// <devdoc>
        /// Collection of parameters used in Delete().
        /// </devdoc>
        [
        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;
            }
        }


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


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


        /// <devdoc>
        /// Collection of parameters used in the FilterExpression property.
        /// </devdoc>
        [
        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;
            }
        }


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

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


        /// <devdoc>
        /// Collection of values used in Insert().
        /// </devdoc>
        [
        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;
            }
        }

        /// <devdoc>
        /// The format string applied to the names of the old values parameters
        /// </devdoc>
        [
        DefaultValue("{0}"),
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_OldValuesParameterFormatString),
        ]
        public string OldValuesParameterFormatString {
            get {
                return GetView().OldValuesParameterFormatString;
            }
            set {
                GetView().OldValuesParameterFormatString = value;
            }
        }


        /// <devdoc>
        /// Gets/sets the ADO.net managed provider name.
        /// </devdoc>
        [
        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);
                }
            }
        }


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

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


        /// <devdoc>
        /// The command to execute when Select is called on the SqlDataSourceView, requesting the total number of rows.
        /// </devdoc>
        /* 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;
            }
        }*/


        /// <devdoc>
        /// Collection of parameters used in Select().
        /// </devdoc>
        [
        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;
            }
        }


        /// <devdoc>
        /// 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.
        /// </devdoc>
        [
        DefaultValue(""),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_SortParameterName),
        ]
        public string SortParameterName {
            get {
                return GetView().SortParameterName;
            }
            set {
                GetView().SortParameterName = value;
            }
        }

        /// <devdoc>
        /// Gets the SQL data source cache object.
        /// </devdoc>
        private SqlDataSourceCache SqlDataSourceCache {
            get {
                SqlDataSourceCache sqlCache = Cache as SqlDataSourceCache;
                if (sqlCache == null) {
                    throw new NotSupportedException(SR.GetString(SR.SqlDataSource_SqlCacheDependencyNotSupported, ID));
                }
                return sqlCache;
            }
        }

        /// <devdoc>
        /// A semi-colon delimited string indicating which databases to use for the dependency in the format "database1:table1;database2:table2".
        /// </devdoc>
        [
        DefaultValue(""),
        WebCategory("Cache"),
        WebSysDescription(SR.SqlDataSourceCache_SqlCacheDependency),
        ]
        public virtual string SqlCacheDependency {
            get {
                return SqlDataSourceCache.SqlCacheDependency;

            }
            set {
                SqlDataSourceCache.SqlCacheDependency = value;
            }
        }


        /// <devdoc>
        /// The command to execute when Update() is called on the SqlDataSourceView.
        /// </devdoc>
        [
        DefaultValue(""),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_UpdateCommand),
        ]
        public string UpdateCommand {
            get {
                return GetView().UpdateCommand;
            }
            set {
                GetView().UpdateCommand = value;
            }
        }

        /// <devdoc>
        /// The type of the update command (command text or stored procedure).
        /// </devdoc>
        [
        DefaultValue(SqlDataSourceCommandType.Text),
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_UpdateCommandType),
        ]
        public SqlDataSourceCommandType UpdateCommandType {
            get {
                return GetView().UpdateCommandType;
            }
            set {
                GetView().UpdateCommandType = value;
            }
        }


        /// <devdoc>
        /// Collection of parameters used in Update().
        /// </devdoc>
        [
        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;
            }
        }



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


        /// <devdoc>
        /// 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.
        /// </devdoc>
        [
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Deleting),
        ]
        public event SqlDataSourceCommandEventHandler Deleting {
            add {
                GetView().Deleting += value;
            }
            remove {
                GetView().Deleting -= value;
            }
        }

        /// <devdoc>
        /// 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.
        /// </devdoc>
        [
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Filtering),
        ]
        public event SqlDataSourceFilteringEventHandler Filtering {
            add {
                GetView().Filtering += value;
            }
            remove {
                GetView().Filtering -= value;
            }
        }


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


        /// <devdoc>
        /// 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.
        /// </devdoc>
        [
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Inserting),
        ]
        public event SqlDataSourceCommandEventHandler Inserting {
            add {
                GetView().Inserting += value;
            }
            remove {
                GetView().Inserting -= value;
            }
        }


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


        /// <devdoc>
        /// 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.
        /// </devdoc>
        [
        WebCategory("Data"),
        WebSysDescription(SR.SqlDataSource_Selecting),
        ]
        public event SqlDataSourceSelectingEventHandler Selecting {
            add {
                GetView().Selecting += value;
            }
            remove {
                GetView().Selecting -= value;
            }
        }


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


        /// <devdoc>
        /// 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.
        /// </devdoc>
        [
        WebCategory("Data"),
        WebSysDescription(SR.DataSource_Updating),
        ]
        public event SqlDataSourceCommandEventHandler Updating {
            add {
                GetView().Updating += value;
            }
            remove {
                GetView().Updating -= value;
            }
        }


        /// <devdoc>
        /// Creates a unique cache key for this data source's data.
        /// </devdoc>
        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();
        }

        /// <devdoc>
        /// Creates a DbConnection to the database based on the ProviderName.
        /// </devdoc>
        internal DbConnection CreateConnection(string connectionString) {
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbConnection connection = factory.CreateConnection();
            connection.ConnectionString = connectionString;
            return connection;
        }

        /// <devdoc>
        /// Creates a DbCommand based on the ProviderName.
        /// </devdoc>
        internal DbCommand CreateCommand(string commandText, DbConnection connection) {
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbCommand command = factory.CreateCommand();
            command.CommandText = commandText;
            command.Connection = connection;
            return command;
        }

        /// <devdoc>
        /// Creates an DbDataAdapter based on the ProviderName.
        /// </devdoc>
        internal DbDataAdapter CreateDataAdapter(DbCommand command) {
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbDataAdapter dataAdapter = factory.CreateDataAdapter();
            dataAdapter.SelectCommand = command;
            return dataAdapter;
        }

        /// <devdoc>
        /// Creates a SqlDataSourceView. Derived classes should override this if they need to return
        /// custom view types.
        /// </devdoc>
        protected virtual SqlDataSourceView CreateDataSourceView(string viewName) {
            return new SqlDataSourceView(this, viewName, Context);
        }

        /// <devdoc>
        /// Creates the cache key for the master (parent) cache entry, which holds the total row count.
        /// </devdoc>
        internal string CreateMasterCacheKey() {
            return CreateRawCacheKey().ToString();
        }

        /// <devdoc>
        /// Creates an IDataParameter based on the ProviderName.
        /// </devdoc>
        internal DbParameter CreateParameter(string parameterName, object parameterValue) {
            DbProviderFactory factory = GetDbProviderFactorySecure();
            DbParameter parameter = factory.CreateParameter();
            parameter.ParameterName = parameterName;
            parameter.Value = parameterValue;
            return parameter;
        }

        /// <devdoc>
        /// Returns the string for the raw master cache key.
        /// </devdoc>
        [SuppressMessage("Microsoft.Usage", "CA2303:FlagTypeGetHashCode", Justification = "This is specifically on SqlDataSource type which is not a com interop type.")]
        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;
        }

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

        /// <devdoc>
        /// 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.
        /// </devdoc>
        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);
            }
        }

        /// <devdoc>
        /// Gets the DbProviderFactory and performs a security check.
        /// </devdoc>
        private DbProviderFactory GetDbProviderFactorySecure() {
            if (_providerFactory == null) {
                _providerFactory = GetDbProviderFactory();

                Debug.Assert(_providerFactory != null);

                if (!HttpRuntime.DisableProcessRequestInApplicationTrust) {
                    // Perform security check if we're not running in application trust
                    if (!HttpRuntime.ProcessRequestInApplicationTrust && !HttpRuntime.HasDbPermission(_providerFactory)) {
                        throw new HttpException(SR.GetString(SR.SqlDataSource_NoDbPermission, _providerFactory.GetType().Name, ID));
                    }
                }
            }
            return _providerFactory;
        }

        /// <devdoc>
        /// Dynamically creates the default (and only) SqlDataSourceView on demand.
        /// </devdoc>
        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;
        }

        /// <devdoc>
        /// Gets the view associated with this connection.
        /// SqlDataSource only supports a single view, so the viewName parameter is ignored.
        /// </devdoc>
        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();
        }

        /// <devdoc>
        /// Returns an ICollection of the names of all the views. In this case there is only one view called "Table".
        /// </devdoc>
        protected override ICollection GetViewNames() {
            if (_viewNames == null) {
                _viewNames = new string[1] { DefaultViewName };
            }
            return _viewNames;
        }

        /// <devdoc>
        /// Inserts a new row with names and values specified the InsertValues collection.
        /// </devdoc>
        public int Insert() {
            return GetView().Insert(null);
        }
        
        /// <devdoc>
        /// Invalidates a cache entry.
        /// </devdoc>
        internal void InvalidateCacheEntry() {
            string key = CreateMasterCacheKey();
            DataSourceCache cache = Cache;
            Debug.Assert(cache != null);
            cache.Invalidate(key);
        }
        
        /// <devdoc>
        /// Event handler for the Page's LoadComplete event.
        /// Updates the parameters' values to possibly raise a DataSourceChanged event, causing bound controls to re-databind.
        /// </devdoc>
        private void LoadCompleteEventHandler(object sender, EventArgs e) {
            SelectParameters.UpdateValues(Context, this);
            FilterParameters.UpdateValues(Context, this);
        }

        /// <devdoc>
        /// Loads data from the cache.
        /// </devdoc>
        internal object LoadDataFromCache(int startRowIndex, int maximumRows) {
            string key = CreateCacheKey(startRowIndex, maximumRows);
            return Cache.LoadDataFromCache(key);
        }
        
        /// <devdoc>
        /// Loads data from the cache.
        /// </devdoc>
        internal int LoadTotalRowCountFromCache() {
            string key = CreateMasterCacheKey();
            object data = Cache.LoadDataFromCache(key);
            if (data is int)
                return (int)data;
            return -1;
        }

        /// <devdoc>
        /// Loads view state.
        /// </devdoc>
        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);
                }
            }
        }

        /// <devdoc>
        /// Adds LoadComplete event handler to the page.
        /// </devdoc>
        protected internal override void OnInit(EventArgs e) {
            base.OnInit(e);

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

        /// <devdoc>
        /// Saves paged data to cache, creating a dependency on the updated row count
        /// </devdoc>
        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);
        }

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

        /// <devdoc>
        /// Saves view state.
        /// </devdoc>
        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;
        }

        /// <devdoc>
        /// 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.
        /// </devdoc>
        public IEnumerable Select(DataSourceSelectArguments arguments) {
            return GetView().Select(arguments);
        }

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

            if (_view != null) {
                ((IStateManager)_view).TrackViewState();
            }
        }

        /// <devdoc>
        /// Updates rows matching the parameters specified in the UpdateParameters collection with new values specified the UpdateValues collection.
        /// </devdoc>
        public int Update() {
            return GetView().Update(null, null, null);
        }
    }
}