programming4us
programming4us
WEBSITE

Advanced ASP.NET : Data-Access Components (part 2) - Using the Data-Access Component

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

2. Using the Data-Access Component

To use this component in a web application, you first have to make sure the appropriate connection string is configured in the web.config file, as shown here:

<configuration>

<connectionStrings>
<add name="AdBoard" connectionString=
"Data Source=localhost\SQLEXPRESS;Initial Catalog=AdBoard;Integrated Security=SSPI" />
</connectionStrings>
...
</configuration>


Next, compile and copy the component DLL file, or add a reference to it if you're using Visual Studio. The only remaining task is to add the user interface for the web page that uses the component.

To test this component, you can create a simple test page. In the example shown in Figure 2, this page allows users to browse the current listing by category and add new items. When the user first visits the page, it prompts the user to select a category.

Figure 2. The AdBoard listing

Once a category is chosen, the matching items display, and a panel of controls appears, which allows the user to add a new entry to the AdBoard under the current category, as shown in Figure 3.

Figure 3. The AdBoard listing

In order to access the component more easily, the web page imports its namespace:

Imports DatabaseComponent

The page code creates the component to retrieve information from the database and displays it by binding the DataSet to the drop-down list or GridView control:

Public Partial Class AdBoard
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack
Dim DB As New DBUtil()

lstCategories.DataSource = DB.GetCategories()
lstCategories.DataTextField = "Name"
lstCategories.DataValueField = "ID"
lstCategories.DataBind()

pnlNew.Visible = False
End If
End Sub

Protected Sub cmdDisplay_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles cmdDisplay.Click

Dim DB As New DBUtil()

gridItems.DataSource = DB.GetItems( _
Val(lstCategories.SelectedItem.Value))
gridItems.DataBind()
pnlNew.Visible = True
End Sub

Protected Sub cmdAdd_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles cmdAdd.Click

Dim DB As New DBUtil()

Try
DB.AddItem(txtTitle.Text, txtDescription.Text, _
Val(txtPrice.Text), Val(lstCategories.SelectedItem.Value))

gridItems.DataSource = DB.GetItems( _
Val(lstCategories.SelectedItem.Value))
gridItems.DataBind()
Catch err As FormatException
' An error occurs if the user has entered an
' invalid price (non-numeric characters).
' In this case, take no action.
' Another option is to add a validator control
' for the price text box to prevent invalid input.
End Try
End Sub
End Class


2.1. Dissecting the Code . . .
  • Not all the functionality of the component is used in this page. For example, the page doesn't use the AddCategory() method or the version of GetItems() that doesn't require a category number. This is completely normal. Other pages may use different features from the component.

  • The code for the web page is free of data access code. It does, however, need to understand how to use a DataSet, and it needs to know specific field names to create a more attractive GridView with custom templates for layout (instead of automatically generated columns).

  • The page could be improved with error handling code or validation controls. As it is, no validation is performed to ensure that the price is numeric or even to ensure that the required values are supplied.

If you're debugging your code in Visual Studio, you'll find you can single-step from your web page code right into the code for the component, even if it isn't a part of the same solution. The appropriate source code file is loaded into your editor automatically, as long as it's available (and you've compiled the component in debug mode).

Other  
 
Top 10
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us