In the sample project (Download Here), you can see two sample usages of this architecture, a simple example without adding paramters (Listing 4) and the more complex usage subscribing to the BuildingParameters event (Listing 5). Observe how the LoadParameters method is added as an event handler for the BuildingParameters event in Listing 5. It is a bit cumbersome to create a method when we could just have one line of code (in this case); doing this in-line will be possible with anonymous methods coming in Whidbey.
Listing 4
1: private void LoadStates()
2: {
3:
4: NameValuePairCollection states =
5: new NameValuePairCollection();
6:
7: states.FillFromDb(MenuCommands.StateMenu,
8: MenuCommands.DefaultConnectionString);
9:
10: states.BindToListControl(this.DropDownList1);
11: }
Listing 5
1: private void LoadAuthors()
2: {
3:
4: NameValuePairCollection authors =
5: new NameValuePairCollection();
6:
7:
8: authors.BuildingParameters +=
9: new NameValuePairCollection.BuildingParametersHandler(
this.LoadParameters);
10:
11: authors.FillFromDb(MenuCommands.AuthorByStateMenu,
12: MenuCommands.DefaultConnectionString);
13:
14: authors.BindToListControl(this.DropDownList2);
15: }
16:
17: private void LoadParameters(object sender,
NameValuePairCollection.BuildingParametersArgs e)
18: {
19:
20:
21: if (e.CommandName == MenuCommands.AuthorByStateMenu)
22: {
23:
24:
25: e.Parameters.Add(
26: new System.Data.SqlClient.SqlParameter("@State",
27: this.DropDownList1.SelectedValue));
28: }
29: }
You may notice the use of the MenuCommands type. This is just an extra I added on at the last minute to give me strongly-typed options for specifying the different stored procedure names and database connections to be used in conjunction with with the LoadFromDb method calls.
Also note that for the sample to run, you'll probably need to update the connection string. It is currently set to use integrated authentication to the default local SQL Server instance, and the application is set to use Windows authentication with impersonation (with all the gotchas that this implies).