Home
Manage Your Code
Snippet: Generic Method for Loading Collections by DataRow (uses DBHelper) (C#)
Title: Generic Method for Loading Collections by DataRow (uses DBHelper) Language: C#
Description: This method allows you to add DataRows and have them call the implemented method on that inherits Collectoin Wrapper to add it as a child object. Views: 437
Author: Nathan Blevins Date Added: 1/22/2008
Copy Code  
1			/// <summary>
2			/// Accepts a collectionwrapper object defined by the collection wrapper generic and loads
3			/// the appropriate data from a dataset into the collection, creating child items in the collection
4			/// for every data row that cooresponds.
5			/// </summary>
6			/// <typeparam name="T">The type of collection wrapper you wish to load.</typeparam>
7			/// <typeparam name="T">The type of items within the collection.</typeparam>
8			/// <param name="Collection">The collection wrapper that is implemented and whose Load method is called.</param>
9			/// <param name="Statement">The sql statment or stored procedure name. </param>
10			/// <param name="Params">The parameters for the sqlstatement / stored procedure.</param>
11			/// <param name="SQLCommandType">The comman type: either Text or StoredProcedure.</param>
12			/// <returns></returns>
13			public static T FillCollectionWrapperObject<T, K>(T Collection, string Statement, SqlParameter[] Params, CommandType SQLCommandType) where T:CollectionWrapper<K>
14			{
15				DataSet ds = FillDataset(Statement, Params, SQLCommandType);
16
17				if (DataSetHasRows(ds))
18				{
19					foreach (DataRow dr in ds.Tables[0].Rows)
20					{
21						Collection.AppendItemFromDataRow(dr);
22					}
23				}
24				else { Collection = null; }
25
26				return Collection;
27			}
28		}
29