SqlDataSourceView.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. //
  2. // System.Web.UI.WebControls.SqlDataSourceView
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sanjay Gupta ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // (C) Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Text;
  35. using System.Data;
  36. using System.ComponentModel;
  37. using System.Data.Common;
  38. namespace System.Web.UI.WebControls {
  39. public class SqlDataSourceView : DataSourceView, IStateManager {
  40. HttpContext context;
  41. DbProviderFactory factory;
  42. DbConnection connection;
  43. public SqlDataSourceView (SqlDataSource owner, string name, HttpContext context)
  44. : base (owner, name)
  45. {
  46. this.owner = owner;
  47. this.name = name;
  48. this.context = context;
  49. }
  50. void InitConnection ()
  51. {
  52. if (factory == null) factory = owner.GetDbProviderFactoryInternal ();
  53. if (connection == null) {
  54. connection = factory.CreateConnection ();
  55. connection.ConnectionString = owner.ConnectionString;
  56. }
  57. }
  58. public int Delete (IDictionary keys, IDictionary oldValues)
  59. {
  60. return ExecuteDelete (keys, oldValues);
  61. }
  62. protected override int ExecuteDelete (IDictionary keys, IDictionary oldValues)
  63. {
  64. if (!CanDelete)
  65. throw new NotSupportedException("Delete operation is not supported");
  66. if (oldValues == null && ConflictDetection == ConflictOptions.CompareAllValues)
  67. throw new InvalidOperationException ("oldValues parameters should be specified when ConflictOptions is set to CompareAllValues");
  68. InitConnection ();
  69. DbCommand command = factory.CreateCommand ();
  70. command.CommandText = DeleteCommand;
  71. command.Connection = connection;
  72. if (DeleteCommandType == SqlDataSourceCommandType.Text)
  73. command.CommandType = CommandType.Text;
  74. else
  75. command.CommandType = CommandType.StoredProcedure;
  76. IDictionary oldDataValues;
  77. if (ConflictDetection == ConflictOptions.CompareAllValues) {
  78. oldDataValues = new Hashtable ();
  79. if (keys != null) {
  80. foreach (DictionaryEntry de in keys)
  81. oldDataValues [de.Key] = de.Value;
  82. }
  83. if (oldValues != null) {
  84. foreach (DictionaryEntry de in oldValues)
  85. oldDataValues [de.Key] = de.Value;
  86. }
  87. }
  88. else
  89. oldDataValues = keys;
  90. InitializeParameters (command, DeleteParameters, null, oldDataValues, true);
  91. SqlDataSourceCommandEventArgs args = new SqlDataSourceCommandEventArgs (command);
  92. OnDeleting (args);
  93. if (args.Cancel)
  94. return -1;
  95. bool closed = connection.State == ConnectionState.Closed;
  96. if (closed)
  97. connection.Open();
  98. Exception exception = null;
  99. int result = -1;;
  100. try {
  101. result = command.ExecuteNonQuery();
  102. } catch (Exception e) {
  103. exception = e;
  104. }
  105. if (closed)
  106. connection.Close ();
  107. OnDataSourceViewChanged (EventArgs.Empty);
  108. SqlDataSourceStatusEventArgs deletedArgs =
  109. new SqlDataSourceStatusEventArgs (command, result, exception);
  110. OnDeleted (deletedArgs);
  111. if (exception != null && !deletedArgs.ExceptionHandled)
  112. throw exception;
  113. return result;
  114. }
  115. public int Insert (IDictionary values)
  116. {
  117. return ExecuteInsert (values);
  118. }
  119. protected override int ExecuteInsert (IDictionary values)
  120. {
  121. if (!CanInsert)
  122. throw new NotSupportedException ("Insert operation is not supported");
  123. InitConnection ();
  124. DbCommand command = factory.CreateCommand ();
  125. command.CommandText = InsertCommand;
  126. command.Connection = connection;
  127. if (InsertCommandType == SqlDataSourceCommandType.Text)
  128. command.CommandType = CommandType.Text;
  129. else
  130. command.CommandType = CommandType.StoredProcedure;
  131. InitializeParameters (command, InsertParameters, values, null, false);
  132. SqlDataSourceCommandEventArgs args = new SqlDataSourceCommandEventArgs (command);
  133. OnInserting (args);
  134. if (args.Cancel)
  135. return -1;
  136. bool closed = connection.State == ConnectionState.Closed;
  137. if (closed)
  138. connection.Open ();
  139. Exception exception = null;
  140. int result = -1;
  141. try {
  142. result = command.ExecuteNonQuery ();
  143. }
  144. catch (Exception e) {
  145. exception = e;
  146. }
  147. if (closed)
  148. connection.Close ();
  149. OnDataSourceViewChanged (EventArgs.Empty);
  150. OnInserted (new SqlDataSourceStatusEventArgs (command, result, exception));
  151. if (exception != null)
  152. throw exception;
  153. return result;
  154. }
  155. public IEnumerable Select (DataSourceSelectArguments arguments)
  156. {
  157. return ExecuteSelect (arguments);
  158. }
  159. protected internal override IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
  160. {
  161. if (SortParameterName.Length > 0 && SelectCommandType == SqlDataSourceCommandType.Text)
  162. throw new NotSupportedException ("The SortParameterName property is only supported with stored procedure commands in SqlDataSource");
  163. if (arguments.SortExpression.Length > 0 && owner.DataSourceMode == SqlDataSourceMode.DataReader)
  164. throw new NotSupportedException ("SqlDataSource cannot sort. Set DataSourceMode to DataSet to enable sorting.");
  165. if (arguments.StartRowIndex > 0 || arguments.MaximumRows > 0)
  166. throw new NotSupportedException ("SqlDataSource does not have paging enabled. Set the DataSourceMode to DataSet to enable paging.");
  167. if (FilterExpression.Length > 0 && owner.DataSourceMode == SqlDataSourceMode.DataReader)
  168. throw new NotSupportedException ("SqlDataSource only supports filtering when the data source's DataSourceMode is set to DataSet.");
  169. InitConnection ();
  170. DbCommand command = factory.CreateCommand ();
  171. command.CommandText = SelectCommand;
  172. command.Connection = connection;
  173. if (SelectCommandType == SqlDataSourceCommandType.Text)
  174. command.CommandType = CommandType.Text;
  175. else {
  176. command.CommandType = CommandType.StoredProcedure;
  177. if (SortParameterName.Length > 0 && arguments.SortExpression.Length > 0)
  178. command.Parameters.Add (CreateDbParameter (SortParameterName, arguments.SortExpression));
  179. }
  180. if (SelectParameters.Count > 0)
  181. InitializeParameters (command, SelectParameters, null, null, false);
  182. Exception exception = null;
  183. if (owner.DataSourceMode == SqlDataSourceMode.DataSet) {
  184. DataView dataView = null;
  185. if (owner.EnableCaching)
  186. dataView = (DataView) owner.Cache.GetCachedObject (SelectCommand, SelectParameters);
  187. if (dataView == null) {
  188. SqlDataSourceSelectingEventArgs selectingArgs = new SqlDataSourceSelectingEventArgs (command, arguments);
  189. OnSelecting (selectingArgs);
  190. if (selectingArgs.Cancel) {
  191. return null;
  192. }
  193. try {
  194. DbDataAdapter adapter = factory.CreateDataAdapter ();
  195. DataSet dataset = new DataSet ();
  196. adapter.SelectCommand = command;
  197. adapter.Fill (dataset, name);
  198. dataView = dataset.Tables [0].DefaultView;
  199. if (dataView == null)
  200. throw new InvalidOperationException ();
  201. }
  202. catch (Exception e) {
  203. exception = e;
  204. }
  205. int rowsAffected = (dataView == null) ? 0 : dataView.Count;
  206. SqlDataSourceStatusEventArgs selectedArgs = new SqlDataSourceStatusEventArgs (command, rowsAffected, exception);
  207. OnSelected (selectedArgs);
  208. if (exception != null && !selectedArgs.ExceptionHandled)
  209. throw exception;
  210. if (owner.EnableCaching)
  211. owner.Cache.SetCachedObject (SelectCommand, selectParameters, dataView);
  212. }
  213. if (SortParameterName.Length == 0 || SelectCommandType == SqlDataSourceCommandType.Text)
  214. dataView.Sort = arguments.SortExpression;
  215. if (FilterExpression.Length > 0) {
  216. IOrderedDictionary fparams = FilterParameters.GetValues (context, owner);
  217. SqlDataSourceFilteringEventArgs fargs = new SqlDataSourceFilteringEventArgs (fparams);
  218. OnFiltering (fargs);
  219. if (!fargs.Cancel) {
  220. object [] formatValues = new object [fparams.Count];
  221. for (int n = 0; n < formatValues.Length; n++) {
  222. formatValues [n] = fparams [n];
  223. if (formatValues [n] == null) return dataView;
  224. }
  225. dataView.RowFilter = string.Format (FilterExpression, formatValues);
  226. }
  227. }
  228. return dataView;
  229. }
  230. else {
  231. SqlDataSourceSelectingEventArgs selectingArgs = new SqlDataSourceSelectingEventArgs (command, arguments);
  232. OnSelecting (selectingArgs);
  233. if (selectingArgs.Cancel) {
  234. return null;
  235. }
  236. DbDataReader reader = null;
  237. bool closed = connection.State == ConnectionState.Closed;
  238. if (closed)
  239. connection.Open ();
  240. try {
  241. reader = command.ExecuteReader (closed ? CommandBehavior.CloseConnection : CommandBehavior.Default);
  242. }
  243. catch (Exception e) {
  244. exception = e;
  245. }
  246. SqlDataSourceStatusEventArgs selectedArgs =
  247. new SqlDataSourceStatusEventArgs (command, reader.RecordsAffected, exception);
  248. OnSelected (selectedArgs);
  249. if (exception != null && !selectedArgs.ExceptionHandled)
  250. throw exception;
  251. return reader;
  252. }
  253. }
  254. public int Update (IDictionary keys, IDictionary values, IDictionary oldValues)
  255. {
  256. return ExecuteUpdate (keys, values, oldValues);
  257. }
  258. protected override int ExecuteUpdate (IDictionary keys, IDictionary values, IDictionary oldValues)
  259. {
  260. if (!CanUpdate)
  261. throw new NotSupportedException ("Update operation is not supported");
  262. if (oldValues == null && ConflictDetection == ConflictOptions.CompareAllValues)
  263. throw new InvalidOperationException ("oldValues parameters should be specified when ConflictOptions is set to CompareAllValues");
  264. InitConnection ();
  265. DbCommand command = factory.CreateCommand ();
  266. command.CommandText = UpdateCommand;
  267. command.Connection = connection;
  268. if (UpdateCommandType == SqlDataSourceCommandType.Text)
  269. command.CommandType = CommandType.Text;
  270. else
  271. command.CommandType = CommandType.StoredProcedure;
  272. IDictionary oldDataValues;
  273. if (ConflictDetection == ConflictOptions.CompareAllValues) {
  274. oldDataValues = new OrderedDictionary ();
  275. if (keys != null) {
  276. foreach (DictionaryEntry de in keys)
  277. oldDataValues [de.Key] = de.Value;
  278. }
  279. if (oldValues != null) {
  280. foreach (DictionaryEntry de in oldValues)
  281. oldDataValues [de.Key] = de.Value;
  282. }
  283. }
  284. else {
  285. oldDataValues = keys;
  286. }
  287. IDictionary dataValues = values;
  288. InitializeParameters (command, UpdateParameters, dataValues, oldDataValues, ConflictDetection == ConflictOptions.OverwriteChanges);
  289. SqlDataSourceCommandEventArgs args = new SqlDataSourceCommandEventArgs (command);
  290. OnUpdating (args);
  291. if (args.Cancel)
  292. return -1;
  293. bool closed = connection.State == ConnectionState.Closed;
  294. if (closed)
  295. connection.Open ();
  296. Exception exception = null;
  297. int result = -1;
  298. try {
  299. result = command.ExecuteNonQuery ();
  300. }
  301. catch (Exception e) {
  302. exception = e;
  303. }
  304. if (closed)
  305. connection.Close ();
  306. OnDataSourceViewChanged (EventArgs.Empty);
  307. SqlDataSourceStatusEventArgs updatedArgs =
  308. new SqlDataSourceStatusEventArgs (command, result, exception);
  309. OnUpdated (updatedArgs);
  310. if (exception != null && !updatedArgs.ExceptionHandled)
  311. throw exception;
  312. return result;
  313. }
  314. string FormatOldParameter (string name)
  315. {
  316. string f = OldValuesParameterFormatString;
  317. if (f.Length > 0)
  318. return String.Format (f, name);
  319. else
  320. return name;
  321. }
  322. object FindValueByName (string parameterName, IDictionary values, bool format)
  323. {
  324. if (values == null)
  325. return null;
  326. foreach (DictionaryEntry de in values) {
  327. string valueName = format == true ? FormatOldParameter (de.Key.ToString ()) : de.Key.ToString ();
  328. if (parameterName == valueName)
  329. return values [de.Key];
  330. }
  331. foreach (DictionaryEntry de in values) {
  332. string valueName = format == true ? FormatOldParameter (de.Key.ToString ()) : de.Key.ToString ();
  333. valueName = valueName.ToLower ();
  334. if (parameterName.ToLower () == valueName)
  335. return values [de.Key];
  336. }
  337. return null;
  338. }
  339. void InitializeParameters (DbCommand command, ParameterCollection parameters, IDictionary values, IDictionary oldValues, bool parametersMayMatchOldValues)
  340. {
  341. IOrderedDictionary parameterValues = parameters.GetValues (context, owner);
  342. foreach (string parameterName in parameterValues.Keys) {
  343. Parameter p = parameters [parameterName];
  344. object value = FindValueByName (parameterName, values, false);
  345. string valueName = parameterName;
  346. if (value == null)
  347. value = FindValueByName (parameterName, oldValues, true);
  348. if (value == null && parametersMayMatchOldValues) {
  349. value = FindValueByName (parameterName, oldValues, false);
  350. valueName = FormatOldParameter (parameterName);
  351. }
  352. if (value == null) {
  353. value = parameterValues [parameterName];
  354. valueName = parameterName;
  355. }
  356. if (value != null) {
  357. object dbValue = p.ConvertValue (value);
  358. DbParameter newParameter = CreateDbParameter (valueName, dbValue, p.Direction, p.Size);
  359. if (!command.Parameters.Contains (newParameter.ParameterName)) {
  360. command.Parameters.Add (newParameter);
  361. }
  362. }
  363. }
  364. if (values != null) {
  365. foreach (DictionaryEntry de in values)
  366. if (!command.Parameters.Contains (ParameterPrefix + (string) de.Key))
  367. command.Parameters.Add (CreateDbParameter ((string) de.Key, de.Value));
  368. }
  369. if (oldValues != null) {
  370. foreach (DictionaryEntry de in oldValues)
  371. if (!command.Parameters.Contains (ParameterPrefix + FormatOldParameter ((string) de.Key)))
  372. command.Parameters.Add (CreateDbParameter (FormatOldParameter ((string) de.Key), de.Value));
  373. }
  374. }
  375. private DbParameter CreateDbParameter (string name, object value)
  376. {
  377. return CreateDbParameter (name, value, ParameterDirection.Input, -1);
  378. }
  379. private DbParameter CreateDbParameter (string name, object value, ParameterDirection dir, int size)
  380. {
  381. DbParameter dbp = factory.CreateParameter ();
  382. dbp.ParameterName = ParameterPrefix + name;
  383. if (value == null)
  384. dbp.Value = DBNull.Value;
  385. else
  386. dbp.Value = value;
  387. dbp.Direction = dir;
  388. if (size != -1)
  389. dbp.Size = size;
  390. return dbp;
  391. }
  392. void IStateManager.LoadViewState (object savedState)
  393. {
  394. LoadViewState (savedState);
  395. }
  396. object IStateManager.SaveViewState ()
  397. {
  398. return SaveViewState ();
  399. }
  400. void IStateManager.TrackViewState ()
  401. {
  402. TrackViewState ();
  403. }
  404. protected virtual void LoadViewState (object savedState)
  405. {
  406. object [] vs = savedState as object [];
  407. if (vs == null)
  408. return;
  409. if (vs [0] != null) ((IStateManager) deleteParameters).LoadViewState (vs [0]);
  410. if (vs [1] != null) ((IStateManager) filterParameters).LoadViewState (vs [1]);
  411. if (vs [2] != null) ((IStateManager) insertParameters).LoadViewState (vs [2]);
  412. if (vs [3] != null) ((IStateManager) selectParameters).LoadViewState (vs [3]);
  413. if (vs [4] != null) ((IStateManager) updateParameters).LoadViewState (vs [4]);
  414. }
  415. protected virtual object SaveViewState ()
  416. {
  417. object [] vs = new object [5];
  418. if (deleteParameters != null) vs [0] = ((IStateManager) deleteParameters).SaveViewState ();
  419. if (filterParameters != null) vs [1] = ((IStateManager) filterParameters).SaveViewState ();
  420. if (insertParameters != null) vs [2] = ((IStateManager) insertParameters).SaveViewState ();
  421. if (selectParameters != null) vs [3] = ((IStateManager) selectParameters).SaveViewState ();
  422. if (updateParameters != null) vs [4] = ((IStateManager) updateParameters).SaveViewState ();
  423. foreach (object o in vs)
  424. if (o != null) return vs;
  425. return null;
  426. }
  427. protected virtual void TrackViewState ()
  428. {
  429. tracking = true;
  430. if (filterParameters != null) ((IStateManager) filterParameters).TrackViewState ();
  431. if (selectParameters != null) ((IStateManager) selectParameters).TrackViewState ();
  432. }
  433. bool IStateManager.IsTrackingViewState {
  434. get { return IsTrackingViewState; }
  435. }
  436. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  437. private bool cancelSelectOnNullParameter = true;
  438. public bool CancelSelectOnNullParameter {
  439. get { return cancelSelectOnNullParameter; }
  440. set { cancelSelectOnNullParameter = value; }
  441. }
  442. public override bool CanDelete {
  443. get { return DeleteCommand != null && DeleteCommand != ""; }
  444. }
  445. public override bool CanInsert {
  446. get { return InsertCommand != null && InsertCommand != ""; }
  447. }
  448. public override bool CanPage {
  449. /* according to MS, this is false in all cases */
  450. get { return false; }
  451. }
  452. public override bool CanRetrieveTotalRowCount {
  453. /* according to MS, this is false in all cases */
  454. get { return false; }
  455. }
  456. public override bool CanSort {
  457. get {
  458. /* we can sort if we're a DataSet, regardless of sort parameter name.
  459. we can sort if we're a DataReader, if the sort parameter name is not null/"".
  460. */
  461. return (owner.DataSourceMode == SqlDataSourceMode.DataSet
  462. || (SortParameterName != null && SortParameterName != ""));
  463. }
  464. }
  465. public override bool CanUpdate {
  466. get { return UpdateCommand != null && UpdateCommand != ""; }
  467. }
  468. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  469. private ConflictOptions conflictDetection = ConflictOptions.OverwriteChanges;
  470. public ConflictOptions ConflictDetection {
  471. get { return conflictDetection; }
  472. set { conflictDetection = value; }
  473. }
  474. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  475. private string deleteCommand = "";
  476. public string DeleteCommand {
  477. get { return deleteCommand; }
  478. set { deleteCommand = value; }
  479. }
  480. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  481. private SqlDataSourceCommandType deleteCommandType = SqlDataSourceCommandType.Text;
  482. public SqlDataSourceCommandType DeleteCommandType {
  483. get { return deleteCommandType; }
  484. set { deleteCommandType = value; }
  485. }
  486. [DefaultValueAttribute (null)]
  487. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  488. [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  489. public ParameterCollection DeleteParameters {
  490. get { return GetParameterCollection (ref deleteParameters, false, false); }
  491. }
  492. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  493. private string filterExpression = "";
  494. public string FilterExpression {
  495. get { return filterExpression; }
  496. set { filterExpression = value; }
  497. }
  498. [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  499. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  500. [DefaultValueAttribute (null)]
  501. public ParameterCollection FilterParameters {
  502. get { return GetParameterCollection (ref filterParameters, true, true); }
  503. }
  504. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  505. private string insertCommand = "";
  506. public string InsertCommand {
  507. get { return insertCommand; }
  508. set { insertCommand = value; }
  509. }
  510. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  511. private SqlDataSourceCommandType insertCommandType = SqlDataSourceCommandType.Text;
  512. public SqlDataSourceCommandType InsertCommandType {
  513. get { return insertCommandType; }
  514. set { insertCommandType = value; }
  515. }
  516. [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  517. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  518. [DefaultValueAttribute (null)]
  519. public ParameterCollection InsertParameters {
  520. get { return GetParameterCollection (ref insertParameters, false, false); }
  521. }
  522. protected bool IsTrackingViewState {
  523. get { return tracking; }
  524. }
  525. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  526. private string oldValuesParameterFormatString = "{0}";
  527. [DefaultValue ("{0}")]
  528. public string OldValuesParameterFormatString {
  529. get { return oldValuesParameterFormatString; }
  530. set { oldValuesParameterFormatString = value; }
  531. }
  532. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  533. private string selectCommand = "";
  534. public string SelectCommand {
  535. get { return selectCommand; }
  536. set { selectCommand = value; }
  537. }
  538. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  539. private SqlDataSourceCommandType selectCommandType = SqlDataSourceCommandType.Text;
  540. public SqlDataSourceCommandType SelectCommandType {
  541. get { return selectCommandType; }
  542. set { selectCommandType = value; }
  543. }
  544. public ParameterCollection SelectParameters {
  545. get { return GetParameterCollection (ref selectParameters, true, true); }
  546. }
  547. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  548. private string sortParameterName = "";
  549. public string SortParameterName {
  550. get { return sortParameterName; }
  551. set { sortParameterName = value; }
  552. }
  553. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  554. private string updateCommand = "";
  555. public string UpdateCommand {
  556. get { return updateCommand; }
  557. set { updateCommand = value; }
  558. }
  559. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  560. private SqlDataSourceCommandType updateCommandType = SqlDataSourceCommandType.Text;
  561. public SqlDataSourceCommandType UpdateCommandType {
  562. get { return updateCommandType; }
  563. set { updateCommandType = value; }
  564. }
  565. [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  566. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  567. [DefaultValueAttribute (null)]
  568. public ParameterCollection UpdateParameters {
  569. get { return GetParameterCollection (ref updateParameters, false, false); }
  570. }
  571. void ParametersChanged (object source, EventArgs args)
  572. {
  573. OnDataSourceViewChanged (EventArgs.Empty);
  574. }
  575. ParameterCollection GetParameterCollection (ref ParameterCollection output, bool propagateTrackViewState, bool subscribeChanged)
  576. {
  577. if (output != null)
  578. return output;
  579. output = new ParameterCollection ();
  580. if(subscribeChanged)
  581. output.ParametersChanged += new EventHandler (ParametersChanged);
  582. if (IsTrackingViewState && propagateTrackViewState)
  583. ((IStateManager) output).TrackViewState ();
  584. return output;
  585. }
  586. protected virtual string ParameterPrefix {
  587. get {
  588. switch (owner.ProviderName) {
  589. case "":
  590. case "System.Data.SqlClient": return "@";
  591. case "System.Data.OracleClient": return ":";
  592. }
  593. return "";
  594. }
  595. }
  596. ParameterCollection deleteParameters;
  597. ParameterCollection filterParameters;
  598. ParameterCollection insertParameters;
  599. ParameterCollection selectParameters;
  600. ParameterCollection updateParameters;
  601. bool tracking;
  602. string name;
  603. SqlDataSource owner;
  604. #region OnDelete
  605. static readonly object EventDeleted = new object ();
  606. protected virtual void OnDeleted (SqlDataSourceStatusEventArgs e)
  607. {
  608. if (!HasEvents ()) return;
  609. SqlDataSourceStatusEventHandler h = Events [EventDeleted] as SqlDataSourceStatusEventHandler;
  610. if (h != null)
  611. h (this, e);
  612. }
  613. public event SqlDataSourceStatusEventHandler Deleted {
  614. add { Events.AddHandler (EventDeleted, value); }
  615. remove { Events.RemoveHandler (EventDeleted, value); }
  616. }
  617. static readonly object EventDeleting = new object ();
  618. protected virtual void OnDeleting (SqlDataSourceCommandEventArgs e)
  619. {
  620. if (!HasEvents ()) return;
  621. SqlDataSourceCommandEventHandler h = Events [EventDeleting] as SqlDataSourceCommandEventHandler;
  622. if (h != null)
  623. h (this, e);
  624. }
  625. public event SqlDataSourceCommandEventHandler Deleting {
  626. add { Events.AddHandler (EventDeleting, value); }
  627. remove { Events.RemoveHandler (EventDeleting, value); }
  628. }
  629. #endregion
  630. #region OnFiltering
  631. static readonly object EventFiltering = new object ();
  632. protected virtual void OnFiltering (SqlDataSourceFilteringEventArgs e)
  633. {
  634. if (!HasEvents ()) return;
  635. SqlDataSourceFilteringEventHandler h = Events [EventFiltering] as SqlDataSourceFilteringEventHandler;
  636. if (h != null)
  637. h (this, e);
  638. }
  639. public event SqlDataSourceFilteringEventHandler Filtering {
  640. add { Events.AddHandler (EventFiltering, value); }
  641. remove { Events.RemoveHandler (EventFiltering, value); }
  642. }
  643. #endregion
  644. #region OnInsert
  645. static readonly object EventInserted = new object ();
  646. protected virtual void OnInserted (SqlDataSourceStatusEventArgs e)
  647. {
  648. if (!HasEvents ()) return;
  649. SqlDataSourceStatusEventHandler h = Events [EventInserted] as SqlDataSourceStatusEventHandler;
  650. if (h != null)
  651. h (this, e);
  652. }
  653. public event SqlDataSourceStatusEventHandler Inserted {
  654. add { Events.AddHandler (EventInserted, value); }
  655. remove { Events.RemoveHandler (EventInserted, value); }
  656. }
  657. static readonly object EventInserting = new object ();
  658. protected virtual void OnInserting (SqlDataSourceCommandEventArgs e)
  659. {
  660. if (!HasEvents ()) return;
  661. SqlDataSourceCommandEventHandler h = Events [EventInserting] as SqlDataSourceCommandEventHandler;
  662. if (h != null)
  663. h (this, e);
  664. }
  665. public event SqlDataSourceCommandEventHandler Inserting {
  666. add { Events.AddHandler (EventInserting, value); }
  667. remove { Events.RemoveHandler (EventInserting, value); }
  668. }
  669. #endregion
  670. #region OnSelect
  671. static readonly object EventSelected = new object ();
  672. protected virtual void OnSelected (SqlDataSourceStatusEventArgs e)
  673. {
  674. if (!HasEvents ()) return;
  675. SqlDataSourceStatusEventHandler h = Events [EventSelected] as SqlDataSourceStatusEventHandler;
  676. if (h != null)
  677. h (this, e);
  678. }
  679. public event SqlDataSourceStatusEventHandler Selected {
  680. add { Events.AddHandler (EventSelected, value); }
  681. remove { Events.RemoveHandler (EventSelected, value); }
  682. }
  683. static readonly object EventSelecting = new object ();
  684. protected virtual void OnSelecting (SqlDataSourceSelectingEventArgs e)
  685. {
  686. if (!HasEvents ()) return;
  687. SqlDataSourceSelectingEventHandler h = Events [EventSelecting] as SqlDataSourceSelectingEventHandler;
  688. if (h != null)
  689. h (this, e);
  690. }
  691. public event SqlDataSourceSelectingEventHandler Selecting {
  692. add { Events.AddHandler (EventSelecting, value); }
  693. remove { Events.RemoveHandler (EventSelecting, value); }
  694. }
  695. #endregion
  696. #region OnUpdate
  697. static readonly object EventUpdated = new object ();
  698. protected virtual void OnUpdated (SqlDataSourceStatusEventArgs e)
  699. {
  700. if (owner.EnableCaching)
  701. owner.Cache.Expire ();
  702. if (!HasEvents ()) return;
  703. SqlDataSourceStatusEventHandler h = Events [EventUpdated] as SqlDataSourceStatusEventHandler;
  704. if (h != null)
  705. h (this, e);
  706. }
  707. public event SqlDataSourceStatusEventHandler Updated {
  708. add { Events.AddHandler (EventUpdated, value); }
  709. remove { Events.RemoveHandler (EventUpdated, value); }
  710. }
  711. static readonly object EventUpdating = new object ();
  712. protected virtual void OnUpdating (SqlDataSourceCommandEventArgs e)
  713. {
  714. if (!HasEvents ()) return;
  715. SqlDataSourceCommandEventHandler h = Events [EventUpdating] as SqlDataSourceCommandEventHandler;
  716. if (h != null)
  717. h (this, e);
  718. }
  719. public event SqlDataSourceCommandEventHandler Updating {
  720. add { Events.AddHandler (EventUpdating, value); }
  721. remove { Events.RemoveHandler (EventUpdating, value); }
  722. }
  723. #endregion
  724. }
  725. }
  726. #endif