Manipulating data in asp.net 2.0: Declaring parameters _ self-study process

Source: Internet
Author: User

Introduction

In the tutorials in the previous chapter, we looked at the GridView, DetailsView, and FormView bindings to the Ojbectdatasource control to display the data. ObjectDataSource called the GetProducts () method of the class PRODUCTSBLL. Method GetProducts () returns a strongly typed datasheet consisting of all the records of a Products table with the Northwind database. The class PRODUCTSBLL also contains other methods for returning partial data: Getproductbyproductid (ProductID), Getproductsbycategoryid (CategoryID), and Getproductsbysupplierid (SupplierID). These three methods require that you enter a parameter to indicate how to filter the returned product information data.

ObjectDataSource can call methods that require incoming arguments, but to do this we need to specify where these parameters get the value. The values of these parameters can be passed in through hard code or dynamically from certain sources, including the value of the query string (querystring), the session variable (sessions variables), the property value of the control on the page, or something else.

In this chapter tutorial, let's start with an example of how to use parameters to set a hard-coded value. In particular, we will look at adding a DetailsView to the page to display a special product called Chef Anton's Gumbo mix, its ProductID is 5. Next, we'll look at how to set the value of the parameter based on the Web control. In particular, we'll use a textbox to have the user type a country (country), and then click a button to list the suppliers in that country.

Using a hard-coded parameter value

The first example is to add a DetailsView control to the Declarativeparams.aspx page in the Basicreporting folder. From the DetailsView control's smart Tag Drop-down list, select < new data Source ..., and then select Add a ObjectDataSource.

Figure 1: Adding a ObjectDataSource to the page

This automatically opens the Configuration data source navigation for the ObjectDataSource control. Select the class PRODUCTSBLL in the first screen navigation.

Figure 2: Select Class Productsbll

Now that we want to display information about a particular product, we should use the Getproductbyproductid (ProductID) method.

Figure 3: Selecting the Getproductbyproductid (ProductID) method

Because we chose this method that contains the parameters, the navigation is one more screen, which requires us to define the values used for the parameters. The list in the left shows all the parameters of the method we specified. For the Getproductbyproductid (ProductID) method, there is only one argument: ProductID. In the parameter source Drop-down list on the right, a number of parameter values that may be used as parameter sources are enumerated. Now that we want to specify a hard-coded value of 5 as the ProductID parameter, let the parameter source option be none and type 5 in the DefaultValue input box.

Figure 4: Parameter ProductID using hard-coded values

When you have finished configuring the Data Source Wizard, the ObjectDataSource control's declaration tag contains a SelectParameters collection, and the arguments for each method specified in each SelectMethod attribute correspond to a parameter object. Since the method in our example requires only one parameter Parameterid, then there is only one element in the SelectParameters collection. The SelectParameters collection 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, and you can also create a custom parameter type if you like (custom parameter types).

<asp:objectdatasource id= "ObjectDataSource1" runat= "Server"

 selectmethod= "Getproductbyproductid" typename= "PRODUCTSBLL" >

 <SelectParameters>

  <asp:parameter defaultvalue= "5" name= "ProductID" type= " Int32 "/>

 </SelectParameters>

</asp:ObjectDataSource>

Note: If you follow these steps on your computer, you may see the parameter values for Insertmethod,updatemethod and DeleteMethod, such as DeleteParameters. The ObjectDataSource Selection Data Source Wizard automatically assigns these PRODUCTBLL methods to insert, update, and delete, so unless you explicitly clear them, they will be included in the tag above.

When you visit this page, the Data Web server control invokes the ObjectDataSource Select method, which invokes the class PRODUCTSBLL Getproductbyproductid (ProductID) and uses the "5" This hard-coded value is ProductID as an input parameter. This method returns a strongly typed Productdatatable object that contains a data row-Chef Anton's Gumbo Mix (ProductID is a product of 5).

Figure 5: Information on displaying chef Anton ' s gumbo mix

To set the properties of a Web control as a parameter source

The parameter value of the Ojbectdatasource control can also be set to be based on the value of the Web control on the page. To illustrate this point, we cite an example of using the GridView to display a vendor in a user-specified country. First, add a TextBox control to the page to let the user enter the country name. The ID property of this textbox control is set to CountryName. Then add a button control to the page.

Figure 6: Adding a TextBox control with ID 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. From the second screens, pick the Getsuppliersbycountry (country) method.

Then, add a GridView control to the page, from its smart tag, select Add a new ObjectDataSource. Because we want to display vendor information, we select Class SUPPLIERSBLL on the first screen of the Configure Data Source Wizard. Second screen, select Getsuppliersbycountry (Country) method.

Figure 7: Selecting the Getsuppliersbycountry (country) method

Because the Getsuppliersbycountry (country) method has an input parameter, the wizard displays the last screen again, allowing you to select the value of the parameter. This time, when you set the parameter source to control, a ControlID drop-down list appears containing the names of all the controls on the page; the CountryName control is selected from. Because the text box CountryName is blank the first time you access this page, there is no value to return 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: Setting the value of the CountryName control as a parameter value

ObjectDataSource's declaration tag is slightly different from our first example. Use a controlparameter to replace the standard parameter object. ControlParameter has some additional attributes to specify the ID of the Web control, as well as a property value (PropertyName) that is used as a parameter. The Configure Data Source wizard is enough to help us decide, such as a textbox, that we will most likely use its Text property as a parameter value. However, if you need to use a different property value for a Web control, you need to change the PropertyName property here, or click "Advanced Properties" from the Configure Data Source Wizard.

<asp:objectdatasource id= "ObjectDataSource2" runat= "Server"

 selectmethod= "Getsuppliersbycountry" typename= "SUPPLIERSBLL" >

 <SelectParameters>

  <asp:controlparameter controlid= "CountryName" Name= " Country "propertyname=" Text "

   type=" String "/>

 </SelectParameters>

</asp:objectdatasource >

When you first access the page, the contents of the text box countryname are blank. The GridView control still invokes the ObjectDataSource select method, but a null value is passed into the Getsuppliersbycountry (country) method. The table adapter (TableAdapter) converts this null value into a null value in the database (DBNull.Value), whereas Getsuppliersbycountry (country) The query used by the method already shows that no results will be returned when a null value dislocation parameter @categoryid is passed in. In short, no suppliers will be returned.

Once the access is entered into a country, and then click the Show Suppliers button to trigger a return (postback), the ObjectDataSource Select method is requested and the text box is passed as the country parameter.

Figure 9: Display the vendor located in Canada

Show all vendors by default

Instead of showing any vendors when you first access the page, we might want to show all the vendors at first, and allow the user to shrink the list by entering the country name in the text box. When the text box is blank, a null value is passed to the Getsuppliersbycountry (country) method of the class SUPPLIERSBLL as the country input parameter. This null value continues to be passed to the Getsupplierbycountry (country) method of the DAL layer, where it is converted to the null value in the database as the @country parameter of the following query:

SELECT  SupplierID, CompanyName, address, City, Country, and Phone from   suppliers

WHERE Country = @Country

Expression country = null always returns FALSE, and no data is returned unless some record country the value of the field is exactly null.

To return all the vendors when the text box is blank, we can add in the Getsuppliersbycountry (country) method of the BLL layer: Call Getsuppliers () when the parameter is country to a null value () Method while others call the Getsuppliersbycountry (country) method of the DAL layer. This results in the following effects: Returning all countries when no country is specified, and returning the appropriate vendor subset when the country parameter is provided.

Change the Getsuppliersbycountry (country) method for class SUPPLIERSBLL, as follows:

Public northwind.suppliersdatatable Getsuppliersbycountry (String country)

{

 if (string). IsNullOrEmpty (country)) return

  getsuppliers ();

 else return

  adapter.getsuppliersbycountry (country);

}

Once this change is made, the first access page declarativeparams.aspx (or when the text box CountryName blank) Displays all of the vendors.

Figure 10: Now, all vendors are displayed by default

Summarize

In order to use a method with input parameters, we need to specify the value of the parameter in the ObjectDataSource SelectParameters collection. Different parameter types allow you to get its value from a different parameter source. The default parameter type uses a hard-coded value, but it can also be very handy to get arguments from querystiring,session variables, cookies, or values entered by a user from a Web control on a page without having to write a line of code.

In this chapter we illustrate how to use parameters in a data source using multiple examples. However, there are times when we may need to use parameters that cannot be empty, such as the current date and time, or if our site uses the user ID of the visitor when membership. In these cases we can programmatically set the parameter values of the method called by ObjectDataSource. In the next chapter of the tutorial, we'll see how to do that.

I wish you a happy programming!

Author Introduction

Scott Mitchell, with six asp/asp. NET book, is the founder of 4GuysFromRolla.com, has been applying Microsoft Web technology since 1998. Scott is an independent technical consultant, trainer, writer, recently completed a new book to be published by Sams Press, 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.