Protected sub gridview1rowupdating
Add the following method to the Products BLL class: _ Public Function Update Product _ (By Val product Name As String, By Val unit Price As Nullable(Of Decimal), _ By Val units In Stock As Nullable(Of Short), By Val product ID As Integer) As Boolean Dim products As Northwind. Also feel free to apply any field-level formatting you deem necessary (such as changing the Header Text properties). While such exception details are helpful when testing an application, presenting an end user with such a screen in the face of an exception is less than ideal. Therefore, the exception won't propagate up to the ASP. Note: Figures 9 and 10 show a graceful way to handle exceptions raised due to invalid user input.In the previous tutorial, we looked at how to format the Unit Price Bound Field as a currency both in read-only mode and edit mode. Recall that this required setting the Bound Field's Data Format String property to , its Html Encode property to false, and its Apply Format In Edit Mode to true, as shown in Figure 2. Configure the Unit Price Bound Field to display as a currency. An end user likely doesn't know what a No Null Allowed Exception is or why it was caused. Keep In Edit Mode = True End If End Sub This event handler begins by checking to see if e. If it's not, the Exception Details Label's Visible property is set to true and its Text property to "There was a problem updating the product." The details of the actual exception that was thrown reside in the e. This inner exception is examined and, if it is of a particular type, an additional, helpful message is appended to the Exception Details Label's Text property. Ideally, though, such invalid input will never reach the Business-Logic Layer in the first place, as the ASP.
Scott Mitchell March 2007 Summary: This is the Visual Basic tutorial. Introduction Step 1: Creating an Editable Grid View of Products Step 2: Gracefully Handling DAL-Level Exceptions Step 3: Gracefully Handling BLL-Level Exceptions Conclusion Working with data from an ASP.
However, for this tutorial, let's allow the user to omit the price. In order to draw the user's eye to this message, set its Css Class property to Warning, which is a CSS class we added to the file in the previous tutorial. Since we want this Label Web control to be visible only immediately after an exception has occurred, set its Visible property to false in the Page_Load event handler: With this code, on the first page visit and subsequent postbacks the Exception Details control will have its Visible property set to false. Text = _ "There are one or more required fields that are missing." Else If Type Of inner Is Argument Exception Then Dim param Name As String = CType(inner, Argument Exception). The validation controls not only prevent the invocation of the Update Product method until the user-supplied data is valid, but also provide a more informative user experience for identifying data-entry problems. Update(product) Return rows Affected = 1 End Function With this change, any price update that is more than twice the existing price will cause an Application Exception to be thrown.
Protected Sub Grid View1_Row Updating(By Val sender As Object, _ By Val e As System. Recall that this CSS class causes the Label's text to be displayed in a red, italic, bold, extra-large font. In the face of a DAL-level or BLL-level exception, which we can detect in the Grid View's Row Updated event handler, we will set the Exception Details control's Visible property to true. Text &= _ "Our database is currently experiencing problems." & _ "Please try again later." Else If Type Of inner _ Is System. When inserting, updating, or deleting data, the Data-Access Layer may throw an exception in the face of a data-related error. Just like the exception raised from the DAL, this BLL-raised Application Exception can be detected and handled in the Grid View's Row Updated event handler.
Formatting the Unit Price as a currency in the editing interface requires creating an event handler for the Grid View's Row Updating event that parses the currency-formatted string into a decimal value. A better approach is to present the user with a more user-friendly message explaining that there were problems attempting to update the product. Lastly, the Exception Handled and Keep In Edit Mode properties are both set to true. NET page should ensure that the user's inputs are valid before invoking the Products BLL class's Update Product method.
Recall that the Row Updating event handler from the last tutorial also checked to ensure that the user provided a Unit Price value. If an exception occurs when performing the operation, the post-level events in both the Object Data Source and the data Web control provide a means to detect it and cancel the exception from bubbling up to the ASP. For our example, let's create an event handler for the Grid View's Row Updated event that determines if an exception has fired and, if so, displays the exception details in a Label Web control. NET page, setting its ID property to Exception Details and clearing out its Text property. Concat("The ", param Name, " value is illegal.") Else If Type Of inner Is Application Exception Then Exception Details. Figure 9 shows a screen shot of this page when omitting the name of the product; Figure 10 shows the results when entering an illegal Unit Price value (-50). In our next tutorial, we'll see how to add validation controls to the editing and inserting interfaces to ensure that the data submitted to the Business-Logic Layer conforms to the business rules. Value End If Dim rows Affected As Integer = Adapter.