Showing All Suppliers By Default
Rather than show none of the suppliers when first viewing the page we may want to show all suppliers at first, allowing the user to pare down the list by entering a country name in the TextBox. When the TextBox is empty, the SuppliersBLL class's GetSuppliersByCountry(country) method is passed in a null value for its country input parameter. This null value is then passed down into the DAL's GetSupplierByCountry(country) method, where it's translated to a database NULL value for the @Country parameter in the following query:
Code:
SELECT SupplierID, CompanyName, Address, City, Country, Phone
FROM Suppliers
WHERE Country = @Country
The expression Country = NULL always returns False, even for records whose Country column has a NULL value; therefore, no records are returned.
To return all suppliers when the country TextBox is empty, we can augment the GetSuppliersByCountry(country) method in the BLL to invoke the GetSuppliers() method when its country parameter is null and to call the DAL's GetSuppliersByCountry(country) method otherwise. This will have the effect of returning all suppliers when no country is specified and the appropriate subset of suppliers when the country parameter is included.
Change the GetSuppliersByCountry(country) method in the SuppliersBLL class to the following:
Code:
public Northwind.SuppliersDataTable GetSuppliersByCountry(string country)
{
if (string.IsNullOrEmpty(country))
return GetSuppliers();
else
return Adapter.GetSuppliersByCountry(country); }
With this change the DeclarativeParams.aspx page shows all of the suppliers when first visited (or whenever the CountryName TextBox is empty).
All Suppliers are Now Shown by Default
Summary
In order to use methods with input parameters, we need to specify the values for the parameters in the ObjectDataSource's SelectParameters collection. Different types of parameters allow for the parameter value to be obtained from different sources. The default parameter type uses a hard-coded value, but just as easily (and without a line of code) parameter values can be obtained from the querystring, Session variables, cookies, and even user-entered values from Web controls on the page.
Bookmarks