Scott Mitchell's ASP. NET 2.0 data tutorial 5: declaring Parameters
Operate data in ASP. NET 2.0: declare Parameters
Download the encoding example in this tutorial | download the PDF version of this tutorial
Introduction
In the tutorial in the previous chapter, we checked the display data of the gridview, detailsview, and formview bound to the ojbectdatasource control. objectdatasource calls the getproducts () method of productsbll. The getproducts () method returns a strongly-typed data table consisting of all records in the products table of the northwind database. The productsbll class also contains other methods for returning part of the data: getproductbyproductid (Productid), Getproductsbycategoryid (Categoryid), And getproductsbysupplierid (Supplierid). The three methods require a parameter to indicate how to filter the returned product information data.
Objectdatasource can call a method that requires passing in parameters. However, to achieve this, we need to specify where to obtain the values of these parameters. The values of these parameters can be passed in through hard code or dynamically obtained from a certain type of source, including query string value (querystring), session variable (Session variables ), the property value of the control on the page, or others.
In this chapter, let's take an example to illustrate how to use parameters to set a hard-coded value. Specifically, we will focus on adding a detailsview to the page to display a special product named chef Anton's gumbo mix. Its productid is 5. Next, we will look at how to set the parameter values based on the web control. In particular, we will use a textbox for users to type country and click a button to list suppliers in that country.
Use a hard-coded parameter value
In the first example, add a detailsview control to the declarativeparams. ASPX page in the basicreporting folder. Select <new data source...> from the Smart Tag drop-down list of the detailsview control, Then add an objectdatasource.
Figure 1: add an objectdatasource to the page
This will automatically open the configuration data source navigation of the objectdatasource control. Select the productsbll class from the navigation bar on the first screen.
Figure 2: Select productsbll
Since we want to display the information of a specific product, we should use getproductbyproductid (Productid) Method.
Figure 3: Select getproductbyproductid (
Productid) Method
Because we chose this method that contains parameters, the navigation will be one more screen. On this screen, we need to define the values used by parameters. All parameters of the method we specified are displayed in the list on the left. For getproductbyproductid (Productid) Method has only one parameter: productid. In the parameter source drop-down list on the right, list the types of parameter values that may be used as the parameter source. Since we want to specify a hard-coded value 5 as the productid parameter, let the parameter source option be none and type 5 in the defaultvalue input box.
Figure 4: The productid parameter uses a hard-coded Value
After the data source Configuration Wizard is complete, the declared tag of the objectdatasource control contains a selectparameters set. The parameters of each method specified in the selectmethod attribute correspond to a parameter object. Since the method in this example only requires one parameter parameterid, there is only one element in the selectparameters set. The selectparameters set can contain any class from the system. Web. UI. webcontrols namespace. The basic parameter class is used because it is a hard-coded parameter value. If necessary, you can also create a custom parameter type (custom parameter types ).
<Asp: objectdatasource id = "objectperformance1" runat = "server"
Selectmethod = "getproductbyproductid" typename = "productsbll">
<Selectparameters>
<Asp: parameter defaultvalue = "5" name = "productid" type = "int32"/>
</Selectparameters>
</ASP: objectdatasource>
Note:If you follow these steps to declare tags on your computer, you may see the parameter values including insertmethod, updatemethod, and deletemethod, such as deleteparameters. The Select data source Wizard of objectdatasource automatically specifies these productbll methods for insertion, update, and deletion. Therefore, they will be included in the above tags unless you clear them explicitly.
When you access this page, the Data Web Server Control calls the select method of objectdatasource, which calls the getproductbyproductid (ProductidAnd use the hard-coded value "5" as the input parameter productid. This method returns a strongly typed productdatatable object that contains information about the chef Anton's gumbo mix (products with productid 5) of a Data row ).
Figure 5: Information about chef Anton's gumbo mix
Set the properties of the web control as the parameter Source
The parameter value of the ojbectdatasource control can also be set to the value of the Web Control on the page. To illustrate this, we will give an example of using the gridview to display suppliers in the country specified by the user. First, add a Textbox Control on the page for users to enter the country name. The ID attribute of the Textbox Control is set to countryname. Add a button control to the page.
Figure 6: Add a Textbox Control with the ID of countryname to the page
Next, add a gridview to the page and, from the smart tag, choose to add a new objectdatasource. since we want to display supplier information select the suppliersbll class from the wizard's first screen. from the second screen, pick the getsuppliersbycountry (Country) Method.
Then, add a gridview control to the page. From its Smart Tag, select Add a new objectdatasource. To display the supplier information, select suppliersbll In the first screen of the data source Configuration Wizard. On the second screen, select getsuppliersbycountry (Country) Method.
Figure 7: Select getsuppliersbycountry (
Country) Method
Because getsuppliersbycountry (Country) Method has an input parameter, so the wizard displays the last screen again, let you select the parameter value. This time, when the parameter source is set to control, a controlid drop-down list is displayed, which contains the names of all controls on the page, and the countryname control is selected. Because the countryname field is blank when you access this page for the first time, no value is returned from it, and no data is displayed. If you want to display some results by default, enter a default parameter value in the defaultvalue text box.
Figure 8: Set the countryname control value as the parameter value
The declaration mark of objectdatasource is slightly different from our first example. A controlparameter replaces the standard parameter object. Controlparameter has some additional properties used to specify the ID of the web control, as well as the property value (propertyname) used as the parameter ). The data source Configuration Wizard is sufficient for us to decide. For example, Textbox, we may use its text attribute as the parameter value. However, if you need to use a different property value for the web control, you need to change the propertyname attribute here, or click "Advanced properties" in the configuration data source wizard ".
<Asp: objectdatasource id = "objectperformance2" runat = "server"
Selectmethod = "getsuppliersbycountry" typename = "suppliersbll">
<Selectparameters>
<Asp: controlparameter controlid = "countryname" name = "country" propertyname = "text"
Type = "string"/>
</Selectparameters>
</ASP: objectdatasource>
When you access this page for the first time, the content in the countryname text box is blank. The gridview control still calls the select method of objectdatasource, but a null value will be passed in getsuppliersbycountry (Country) Method. The table adapter converts the null value to the null value (dbnull. Value) in the database. However, getsuppliersbycountry (Country) The query used by the method already specifies that no result will be returned when a null value misaligned parameter @ categoryid is input. In short, no supplier is returned.
Once the access is entering a country, Click Show suppliers to trigger a return (PostBack). The select method of objectdatasource is requested, and the text value of the text box is passed as the country parameter.
Figure 9: suppliers located in Canada
All vendors are displayed by default.
As opposed to not displaying any suppliers when accessing the page for the first time, we may want to show all vendors at the beginning and allow users to scale down the list by entering country names in the text box. When the text box is blank, a null value is passed to the getsuppliersbycountry (Country) MethodCountryEnter parameters. This null value is passed to the getsupplierbycountry (Country) Method, where it will be converted to the null value in the database as the @ country parameter for the following query:
Select supplierid, companyName, address, city, country, phone
From suppliers
Where Country = @ country
Expression Country = NULL always returns false, unless some records of country field values happen to be null, otherwise no data is returned.
To return all vendors when the text box is blank, we can go to getsuppliersbycountry (Country) Added to the method: when the country parameter is null, The getsuppliers () method is called. Otherwise, the getsuppliersbycountry (Country) Method. This will achieve the following effect: when the country is not specified, all countries are returned, and when the country parameter is provided, an appropriate supplier subset is returned.
Change getsuppliersbycountry (Country) Method, as follows:
Public northwind. suppliersdatatable getsuppliersbycountry (string country)
{
If (string. isnullorempty (country ))
Return getsuppliers ();
Else
Return Adapter. getsuppliersbycountry (country );
}
After this change is made, all vendors are displayed on the declarativeparams. ASPX page (or when the countryname field is blank.
Figure 10: Now, all vendors are displayed by default
Summary
To use a method with input parameters, we need to specify the parameter value in the selectparameters set of objectdatasource. Different parameter types allow you to obtain its values from different parameter sources. The default parameter type uses a hard-coded value, but it can also be very convenient to obtain parameters from querystiring, session variables, cookies, or the values entered by the web control on the page, you do not need to write a line of code.
This chapter illustrates how to use parameters in a data source through multiple examples. However, sometimes we may need to use parameters that cannot be blank, such as the current date and time, or if our site uses the user ID of the visitor when membership is used. In these cases, we can pre-set the parameter values of the methods called by objectdatasource through programming. In the next chapter, we will look at how to implement this.
Happy programming!
Author Profile
Scott Mitchell, author of six ASP/ASP. NET books, is the founder of 4guysfromrolla.com and has been using Microsoft Web technology since 1998. Scott is an independent technical consultant, trainer and writer. He has recently completed a new work to be published by SAMs Publishing House and is proficient in ASP. NET 2.0 within 24 hours. His contact email is mitchell@4guysfromrolla.com, or he can contact him through his blog http://ScottOnWriting.NET.