| Home | About Us | Contact Us | |
Advisory Services | IT Solutions | Free Articles | Training | Resource Center |
![]() |
Title: Master Detail Report Showing Author: James Spruell Email: info@jspruell.com IntroductionThe customer / employee information center provides an individual the opportunity to view their records online to look up order status, invoices, bills and other personal information. In each case, the SQL running in the background retrieves the customer/employee record by matching their logon with that stored in the database. In our example we are using the self-information center concept to demonstrate the
ability to create a more natural business view in ASP.NET using a combination of labels
and Many different approaches are available to handle the data in this type of application.
I chose to use the We automated the record retrieval process by capturing the user logon using Forms
Authentication. In this example we match the value in The example displays personal information about the employee (Master) using labels and
their sales (detail) using a Solution OverviewThe solution requires 3 essential steps:
Pre-code StepsThe Northwind Employees table used in the demonstration must be modified to add a
column to store the logon. For this example, we used the name In the demonstration we have not added any error checking to keep it simple... Using the CodeAt this point we can focus our attention on the ASP.NET code that creates the Master Detail Report. Comments have been included to explain the coding:
' Add the appropriate Namespaces for our project
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
'--------------------------------------------------------------
' Create & manage the cache that handles the Master section
'--------------------------------------------------------------
'---------------------------------------------------------
'con for Connection 'dad for DataAdapter 'dst for DataSet
'dvw for DataView 'cmd for command 'lbl for label
'---------------------------------------------------------
Dim MyCmd as String
Sub Page_Load ( s As Object, e As EventArgs )
If Not Page.IsPostBack Then
BindGrid( )
End If
' Create our Variables
Dim conNorthwind As SqlConnection
Dim dadEmployees As SqlDataAdapter
Dim dstEmployees As DataSet
Dim dvwEmployees As DataView
' Set up the DataView cache
dvwEmployees = Cache( "Employees" )
If dvwEmployees Is Nothing Then
conNorthwind = New SqlConnection( "server=(local);_
database=Northwind;trusted_connection=true" )
dadEmployees = New SqlDataAdapter( "Select * From Employees _
where u_UserName = '" & User.Identity.Name & "' ", conNorthwind )
dstEmployees = New DataSet()
dadEmployees.Fill( dstEmployees, "Employees" )
dvwEmployees = dstEmployees.Tables( "Employees" ).DefaultView()
Cache( "Employees" ) = dvwEmployees
End If
' Display the information using labels
lblEmpID.Text = dvwEmployees( 0 ).Row( "EmployeeID" )
lblFirstName.Text = dvwEmployees( 0 ).Row( "FirstName" )
lblLastName.Text = dvwEmployees( 0 ).Row( "LastName" )
lblPhone.Text = dvwEmployees( 0 ).Row( "HomePhone" )
lblEmail.Text = dvwEmployees( 0 ).Row( "EmailAddress" )
lblNotes.Text = dvwEmployees( 0 ).Row( "Notes" )
End Sub
Step 1 The name Step 2 Step 3 then ... '--------------------------------------------------------------
' Begin the Datagrid section that will handle Report Details
'--------------------------------------------------------------
' Set up Page Index functions
'--------------------------------------------------------------
Sub DataGrid_SetPage(Sender As Object, e As DataGridPageChangedEventArgs)
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub
Sub DataGrid_IndexChanged(sender As Object, e As EventArgs)
End Sub
' Set up application to handle sorting
'---------------------------------------------------------------
Property Sort_Field() As String
Get
Dim obj As Object = ViewState("Sort_Field")
If obj Is Nothing Then
Return String.Empty
End If
Return CStr(obj)
End Get
Set(ByVal Value As String)
ViewState("Sort_Field") = Value
End Set
End Property
Sub DataGrid_Sort(Sender As Object, e As DataGridSortCommandEventArgs)
DataGrid1.CurrentPageIndex = 0
Sort_Field = e.SortExpression
BindGrid()
End Sub
'---------------------------------------------------------------
' Bind the Datagrid
'---------------------------------------------------------------
Sub BindGrid()
' Create a custom SQL statement to get the data
Dim CmdText As String
If Sort_Field = String.Empty Then
CmdText = "select * from Northwind.dbo.Orders where exists _
(Select * From Employees where Employees.EmployeeID _
= Orders.EmployeeID and Employees.u_UserName = '" _
& User.Identity.Name & "' ) order by CustomerID"
MyCmd = CmdText
Else
CmdText = "select * from Northwind.dbo.Orders where exists _
(Select * From Employees where Employees.EmployeeID _
= Orders.EmployeeID and Employees.u_UserName = '" _
& User.Identity.Name & "' ) order by " & Sort_Field
MyCmd = CmdText
End If
'Set the SQLDataAdapter to connect to the Northwind Order table
' & use our custom SQL Statement
Dim conNorthwind2 As New SqlConnection("server=(local);_
database=Northwind;trusted_connection=true")
Dim dadOrders As New SqlDataAdapter(MyCmd, conNorthwind2)
'Create dstOrders (the Order Dataset), fill it, and bind it.
Dim dstOrders As New DataSet()
dadOrders.Fill(dstOrders)
DataGrid1.DataSource = dstOrders
DataGrid1.DataBind()
End Sub
'-----------------------------------------------------------------------
'End of Script, Begin HTML Section Below
'-----------------------------------------------------------------------
How to use itThe demo application includes an ASP.NET page that we have pre-tested on our server... however, there are some changes that must be made prior to using the code:
ConclusionThe application enables a user to logon, and view a Master Detail report of their
personal information & orders that he or she has taken. Master level data is placed in
labels to create a natural looking form, and Detail level data is placed in a The example is for purposes of illustration only... there are many ways to perform the data retrieval, etc. to retrieve the data from a SQL Server database. Downloading the data to the client side allows a number of employees to work with their sales without maintaining an active cursor. We have tried to balance an overview with the necessary details to build this site. From experience, we probably were too detailed for some, and not enough for others. |
|
Site Map
Phone us in Kansas City, Missouri at 816-878-2098
© 2004 by James Spruell All rights reserved