400 likes | 478 Views
M0194 Web-based Programming Lanjut. Session 8. ASP and Data on the Client. Disconnected Recordset Remote Data Services (RDS) Data Transfer between Server and Client Recordset Paging. Disconnected Recordset.
E N D
M0194 Web-based Programming Lanjut Session 8
ASP and Data on the Client • Disconnected Recordset • Remote Data Services (RDS) • Data Transfer between Server and Client • Recordset Paging
Disconnected Recordset • A disconnected recordset is just a normal recordset, but its connection to the server removed. • The recordset stands alone, can act like a normal recordset, allowing updates, additions, and deletions. • But changes only happen in the recordset, are not reflected in the server. • The connection to the server can be re-established and the server updated with any changes • If data on the server has changed, you can detect these changes. This is called conflict resolution.
Remote Data Service (RDS) • RDS is the name given to the set of services that allows us to handle data on the client. • RDS is part of ADO • Only comes into use when you need to transfer and use data at the client • Made up from several components
Client Server Name: Client: Data store Data Binding Manager OLE-DB Provider Data Source Object Data Client Cache DataFactory Object Custom Component DataSpace Object Network Web Server Remote Data Server
Remote Data Server • RDS Server Components • Data store • OLE DB Provider • Two option how work with the data • The DataFactory The default server-side component, provides access to data stores • A Custom Component A normal COM component that has methods to perform data transfer. • Web Server, acts as the interface between client and the server.
Remote Data Server • RDS Client Components • DataSpace Object • goes hand-in-hand with the DataFactory or a Custom Component, provides the client-side part of the equation. • DataSpace is a proxy object, responsible for the communication with the server, providing the channel through which data is moved. • Created by using an HTML <OBJECT> tag. • Data Source Object (DSO) • Responsible for storing data on the client, contains an ADO Recordset and work together with the Client Data Cache to manage the data • Created by using an HTML <OBJECT> tag • Client Data Cache • The client cursor service managing the data on the client • Data Binding Manager • Create the link (binding) between the HTML controls and the DSO. Perform by specifying the DATASRC and DATAFLD attributes of certain HTML controls.
Remote Data Server • Browser Support for RDS • Only works in Microsoft browsers • Fully supported on Microsoft Internet Explorer 4.0 and above • Be aware, clients accessing application that rely on RDS might have different versions with the server. For example, IE4 shipped with RDS 1.5, while IE5, Office2000 and Visual Studio 6 with RDS 2.0, Windows 2000 with RDS 2.5 • Two ways to take care of compability problem : • Make sure all users upgrade to the latest version of RDS. • Specify the Data Factory Mode when connecting to data source. Specify which version to use.
Remote Data Server • Data Source Objects Is a client-side object that stores and manages the data once it is on the client. Several different DSO : • Tabular Data Control (TDC) is design to manipulate data held in a tabular or delimited from text files. • Remote Data Service Data Control is design to connect to OLEDB data stores, and can specify where to connect to as well as which data to return • Java DataBase Connector is a Java applet that allows connection to data stores through the Java DataBase Control (JDBC). • Microsoft HTML (MSHTML) DSO takes data marked up in HTML and uses this as the source of the data. • XML DSO uses XML data, and can be used for either well-structured or arbitrarily structured XML.
Tabular Data Control • TDC is the simplest DSO, useful for small amounts of data that will be read-only. • Created by placing an <OBJECT> tag <OBJECT CLASSID=“clsid:333C7BC4-460F-11D0BC04-0080C7055A83” ID=“dsoEmployee” WIDTH=“0” HEIGHT=“0”> <PARAM NAME=“DataURL” Value=“Employee.csv”> </OBJECT> example of data in Comma Separated Values (CSV):
RDS Data Control • RDS Data Control provides access to normal data stores, as opposed to flat files. • Most often used to connect to SQL database to retrieve data from tables queries or stored procedures • Allow data to be update • Created using an HTML <OBJECT> tag <OBJECT CLASSID=“clsid:BD96C556-65A3-11D0-983A-00C04FC29E33” ID=“dsoAuthors” WIDTH=“0” HEIGHT=“0”> <PARAM NAME=“Connect” Value=“Connection String”> <PARAM NAME=“Server” Value=“Server Name”> <PARAM NAME=“SQL” Value=“Query Text”> </OBJECT>
RDS Data Control • Example : • ADO 2.5 has a new URL property that allows to use file as the source of data. <OBJECT CLASSID=“clsid:BD96C556-65A3-11D0-983A-00C04FC29E33” ID=“dsoAuthors” WIDTH=“0” HEIGHT=“0”> <PARAM NAME=“Connect” Value=“DSN=pubs”> <PARAM NAME=“Server” Value=“W2000”> <PARAM NAME=“SQL” Value=“SELECT * FROM Authors”> </OBJECT> <OBJECT CLASSID=“clsid:BD96C556-65A3-11D0-983A-00C04FC29E33” ID=“dsoAuthors” WIDTH=“0” HEIGHT=“0”> <PARAM NAME=“URL” Value=“DataPage.asp”> </OBJECT>
RDS Data Control • The DataPage.asp file could contain the following VBScript code : <!-- #include file="dataconn.inc" --> <% Dim rsData Set rsData = Server.CreateObject (“ADODB.Recordset”) rsData.Open “SELECT * FROM Authors”, strConn rsData.Save Response, 1 ‘ adPersistXML= 1 rsData.Close Set rsData = Nothing %>
MSHTML Data Control • MSHTML is rather unusual – is part of IE • Allows to have a data source based on HTML. • MSHTML is not a form that naturally use for data storage • MSHTML could be useful if you have a lot of HTML pages that do contain some data. • MSHTML data control has a Recordset property, but has no methods. • Created by <OBJECT> tag <OBJECT ID=“dsoAuthors” DATA=“Authors.html” HEIGHT=“0” WIDTH=“0”> </OBJECT>
MSHTML Data Control • To be able to use the control, your HTML tag must have an ID attribute. • The example above show that the HTML tag names are irrelevant – it’s the ID that is important. • The above HTML, would generate 2 rows of data, that looked like this: <DIV ID=“au_id”>172-32-1176</DIV> <SPAN ID=“au_lname”>White</SPAN> <H1 ID=“au_fname”>Bob</H1> <PRE ID=“au_id”>213-46-8915</PRE> <H2 ID=“au_lname”>Green</H2> <H1 ID=“au_fname”>Cheryl</H1>
XML Data Control • The <XML> tag acts like a data control • Use the <XML> tag in one of two ways: • The first way, use the SRC attribute to specify the location of the data • Alternatively, embed the XML in the tag <XML ID=“dsoAuthors” SRC=“Authors.xml”></XML> <XML ID=“dsoAuthors”> <Authors> <Author> <au_id>172-32-1176</au_id> <au_lname>White</au_lname> <au_fname>Johnson</au_fname> <phone>408 496-7223</phone> <contract>True</contract> </Author> <Author> <au_id>213-46-8915</au_id> <au_lname>Green</au_lname> <au_fname>Marjorie</au_fname> <phone>415 986-7020</phone> <contract>True</contract> </Author> </Authors> </XML>
Data Binding • Binding means setting up a relationship between some HTML elements and the data control. • The data control is responsible for managing the data and supplying the data to the HTML element • The element displays the data on the screen • To bind an HTML element to a data source, specify two attributes for it: • DATASRC, to identify the data control that contains the data. Always put a # in front of the data source name. • DATAFLD, to identify the field in the data control to bind to. The fields are the names identifying the columns in the data that the data control manages. • DATAFORMATS, to indicate how the data in the field should formatted, can be either HTML or TEXT. The default is TEXT
Data Binding <HTML> <HEAD><TITLE>RDSSingleBinding.asp</TITLE></HEAD> <BODY> <H1>Data Binding with RDS<HR></H1> <OBJECT CLASSID="clsid:BD96C556-65A3-11D0-983A-00C04FC29E33" ID="dsoAuthors" WIDTH="0" HEIGHT="0"> <PARAM NAME="URL" Value="DataPage.asp"> </OBJECT> <DIV DATASRC="#dsoAuthors" DATAFLD="au_fname"></DIV> <DIV DATASRC="#dsoAuthors" DATAFLD="au_lname"></DIV> </BODY> </HTML>
Data Binding • <HTML> • <HEAD><TITLE>TDCDataFormat.asp</TITLE></HEAD> • <BODY> • <H1>Data Formatting in bound data<HR></H1> • <OBJECT CLASSID="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83" • ID="dsoImages" WIDTH="0" HEIGHT="0"> • <PARAM NAME="DataURL" Value="images.csv"> • <PARAM NAME = "UseHeader" VALUE = "TRUE"> • </OBJECT> • <TABLE ID="tblData" DATASRC="#dsoImages" BORDER="1"> • <THEAD> • <TR> • <TD>Description</TD> • <TD>Image</TD> • </TR> • </THEAD> • <TBODY> • <TR> • <TD><SPAN DATAFLD="Description"></SPAN></TD> • <TD><SPAN DATAFLD="Image" DATAFORMATAS="HTML"></SPAN></TD> • </TR> • </TBODY> • </TABLE> • </BODY> • </HTML>
Data Binding Images.csv: Description,Image The asian cartoon,<IMG SRC=images\asian_cartoon.gif> The Badz-maru,<IMG SRC=images\badtz-maru.gif> The Businessman,<IMG SRC=images\biz_man.jpg>
Table Binding • Table binding differs from single record binding, because we bind to the TABLE element. This allows to see more than one record at a time. • Table binding has one attribute useful to data binding – DATAPAGESIZE. This determines how many records are shown in the table
Table Binding • <HTML> • <HEAD><TITLE>RDSTableBinding.asp</TITLE></HEAD> • <BODY> • <H1>Table Data Binding with RDS<HR></H1> • <OBJECT CLASSID="clsid:BD96C556-65A3-11D0-983A-00C04FC29E33“ • ID="dsoData" WIDTH="0" HEIGHT="0"> • <PARAM NAME="URL" Value="DataPage.asp"> • </OBJECT> • <TABLE ID="tblData" DATASRC="#dsoData" DATAPAGESIZE="5" > • <THEAD> • <TR> • <TD>au_id</TD> • <TD>au_fname</TD> • <TD>au_lname</TD> • <TD>phone</TD> • <TD>address</TD> • <TD>city</TD> • <TD>state</TD> • <TD>zip</TD> • <TD>contract</TD> • </TR> • </THEAD> • <TBODY> • <TR> • <TD><INPUT TYPE="TEXT" DATAFLD="au_id"></INPUT></TD> • <TD><INPUT TYPE="TEXT" DATAFLD="au_fname"></INPUT></TD> • <TD><INPUT TYPE="TEXT" DATAFLD="au_lname"></INPUT></TD> • <TD><INPUT TYPE="TEXT" DATAFLD="phone"></INPUT></TD> • <TD><INPUT TYPE="TEXT" DATAFLD="address"></INPUT></TD> • <TD><INPUT TYPE="TEXT" DATAFLD="city"></INPUT></TD> • <TD><INPUT TYPE="TEXT" DATAFLD="state"></INPUT></TD> • <TD><INPUT TYPE="TEXT" DATAFLD="zip"></INPUT></TD> • <TD><INPUT TYPE="TEXT" DATAFLD="contract"></INPUT></TD> • </TR> • </TBODY> • </TABLE> • </BODY> • </HTML>
Data Transfer Between Server and Client • Some problem we need to solve • We need a secure way to retrieve data from a server and use it on the client • We need a way that allows data to be updated • We don’t want to compromise flexibility • The way around this problem is to use components. • A component allows us to encapsulate all of our data access functionality, including the source of the data, and ensures that data are only exposed to Web page.
Data Transfer Between Server and Client • A server based component • The component creator uses a programming language that can create COM components specification. For example, Visual Basic(VB) or Visual C++(VC++). • The client has a programming language or tool (Such as Microsoft Word or ASP) that know how to instantiate and use COM components, by following the rules laid down in the COM specification. Example – creating Book Component Start Visual Basic and create a new ActiveX DLL project, changing the name of the default class module to BookTitles. Next, change the name of the project to Book. Add a reference that we need in the project.
Option Explicit • Private mobjConn As ADODB.Connection • Private mrsBooks As ADODB.Recordset • 'open the connection and recordset • Public Sub OpenTitles() • Set mobjConn = New ADODB.Connection • Set mrsBooks = New ADODB.Recordset • mobjConn.ConnectionString="Provider=SQLOLEDB; Data Source=hogwartz; Initial Catalog=pubs; User ID=sa; “ & _ “Password= letmein" • mobjConn.Open • mrsBooks.CursorLocation = adUseClient • mrsBooks.Open "SELECT * FROM Titles", mobjConn • End Sub • 'Close the list • Public Sub CloseTitles() • 'Close the recordset • If Not (mrsBooks Is Nothing) Then • If mrsBooks.State = adStateOpen Then • mrsBooks.Close • End If • Set mrsBooks = Nothing • End If • 'Close the connection • If Not (mobjConn Is Nothing) Then • mobjConn.Close • Set mobjConn = Nothing • End If • End Sub
'returns the next title • Public Sub NextTitle(Id As Variant, Title As Variant, Price As Variant, Notes As Variant) • 'Ignore errors and more specifically null values in this demo • On Error Resume Next • If IsEOF = True Then • Err.Raise vbObject + 1, "BookTitles Component", "End of Cursor" • Exit Sub • End If • 'Copy the fields back to the caller • Id = mrsBooks.Fields("Title_id") • Title = mrsBooks.Fields("Title") • Price = mrsBooks.Fields("Price") • Notes = mrsBooks.Fields("Notes") • 'Move to the next title • If Not mrsBooks.EOF Then mrsBooks.MoveNext • End Sub • Public Function IsEOF() As Boolean • IsEOF = mrsBooks.EOF • End Function If you tried defining the NextTitle procedure Parameter using the String and Currency types, you’d get an error. Because the Active Scripting engine only know the Variant type.
Data Transfer Between Server and Client • <% Dim objTitles • Set objTitles = Server.CreateObject("Book.BookTitles") • %> • <HTML><HEAD><TITLE>Titles List</TITLE></HEAD> • <BODY> • <H1>Book Titles</H1><HR> • <P>The following books are currently defined in the pubs database: • <TABLE cellspacing="2" cellpadding="0"> • <TR> <TD bgcolor="#3AC2EF"><STRONG>ID</STRONG></TD> • <TD bgcolor="#3AC2EF"><STRONG>Title</STRONG></TD> • <TD bgcolor="#3AC2EF"><STRONG>Price</STRONG></TD> • <TD bgcolor="#3AC2EF"><STRONG>Notes</STRONG></TD> </TR> • <TR></TR> • <% • Dim strID • Dim strTitle • Dim strPrice • Dim strNotes • 'Initialize the list • objTitles.OpenTitles • 'Process each author • While objTitles.IsEOF = False • objTitles.NextTitle strID, strTitle, strPrice, strNotes • %> • <TR> <TD bgcolor="#FFFF6C"><%= strID %></TD> • <TD bgcolor="#FFFF6C"><%= strTitle %></TD> • <TD bgcolor="#FFFF6C"><%= strPrice %></TD> • <TD bgcolor="#FFFF6C"><%= strNotes %></TD></TR> • <% Wend %> • </TABLE></BODY></HTML>
Data Transfer Between Server and Client • Advantages of using Server Side Components • There are no connection details visible to the user – which gives you a securer system. The component also has to be specially registered, so there is the added security restriction. • You can build components in any COM compliant language. We use VB in this example, because it’s easy to understand. • Your components isn’t limited to just returning and updating recordsets but you could build in lots of logic. • You could encapsulate all of that conflict resolution code in the Recordset method, and just return details of any errors. This means your client-side script is neater and users can’t rip off your hard-worked code.
Recordset Paging • Using ADO Paging • <% • Dim rsData • Dim intPage • Dim intTotalPages • Dim fldF • Dim intRec • Dim strQuote • Dim strScriptName • Dim strConn • strConn = " Provider = SQLOLEDB; Data Source= Hogwartz; Initial Catalog= pubs; User ID= sa; Password=letmein" • strQuote = Chr(34) 'the double quote character • Set rsData = Server.CreateObject("ADODB.Recordset") • 'set the page size • rsData.PageSize = 10 • rsData.CursorLocation = 3 'adUseClient=3 • 'Open the data • rsData.Open "Authors", strConn,0,1,2 'adOpenForwardOnly=0, adLockReadOnly=1, adCmdTable=2
If Request.QueryString("PAGE")="" Then • intPage=1 • Else • intPage = CInt( Request.QueryString("PAGE") ) • If intPage < 1 then • intPage=1 • Else • If intPage > rsData.PageCount Then • intPage = rsData.PageCount • Else • intPage = CInt( Request.QueryString("PAGE") ) • End If • End If • End If • rsData.AbsolutePage = intPage • Response.Write "Page = " & rsData.AbsolutePage & "<BR>" • Response.Write "<TABLE BORDER=1><THEAD><TR>" • For Each fldF In rsData.Fields • Response.Write "<TD>" & fldF.Name & "</TD>" • Next • Response.Write "</TR></THEAD><TBODY>" • For intRec = 1 to rsData.PageSize • If Not rsData.EOF Then • Response.Write "<TR>" • For Each fldF In rsData.Fields • Response.Write "<TD>" & fldF.Value & "</TD>" • Next • Response.Write "</TR>" • rsData.MoveNext • End If • Next • Response.Write "</TBODY></TABLE></P>"
strScriptName = Request.ServerVariables("SCRIPT_NAME") • Response.Write " <A HREF=" & strQuote & strScriptName & "?PAGE=1" & strQuote & "> First Page </A>" • 'Only give an active previous page if there are previous page • If intPage= 1 Then • Response.Write " <SPAN>Previous Page</SPAN>" • Else • Response.Write " <A HREF=" & strQuote & strScriptName & "?PAGE="& intPage - 1 & strQuote & "> Previous Page </A>" • End If • 'Only give an active next page if there are more pages • If intPage = rsData.PageCount Then • Response.Write " <SPAN>Next Page</SPAN>" • Else • Response.Write " <A HREF=" & strQuote & strScriptName & "?PAGE="& intPage + 1 & strQuote & "> Next Page </A>" • End If • Response.Write " <A HREF=" & strQuote & strScriptName & "?PAGE=" & rsData.PageCount & strQuote & "> LastPage </A>" • rsData.Close • Set rsData =Nothing • %>
Recordset Paging • Using SQL Server CREATE PROCEDURE usp_PagedAuthors @iPage int, @iPageSize int AS BEGIN SET NOCOUNT ON -- disable row counts -- declare variables DECLARE @iStart int -- start record DECLARE @iEnd int -- end record DECLARE @iPageCount int -- total number of pages CREATE TABLE #PagedAuthors -- create the temporary table ( ID int IDENTITY, au_id varchar(11) NOT NULL, au_lname varchar(40) NOT NULL, au_fname varchar(20) NOT NULL, phone char(12) NOT NULL, address varchar(20) NULL, city varchar(20) NULL, state char(2) NULL, zip char(5) NULL, contract bit NOT NULL )
-- populate the temporary table INSERT INTO #PagedAuthors (au_id, au_lname, au_fname, phone, address, city, state, zip, contract) SELECT au_id, au_lname, au_fname, phone, address, city, state, zip, contract FROM authors -- work out how many pages there are in total SELECT @iPageCount = COUNT(*) FROM authors SELECT @iPageCount = 3 -- check the page number IF @iPage < 1 SELECT @iPage = 1 IF @iPage > @iPageCount SELECT @iPage = @iPageCount -- calculate the start and end records SELECT @iStart = (@iPage - 1) * @iPageSize SELECT @iEnd = @iStart + @iPageSize +1 -- select only those records that fall within our page SELECT au_id, au_lname, au_fname, phone, address, city, state, zip, contract FROM #PagedAuthors WHERE ID > @iStart AND ID < @iEnd DROP TABLE #PagedAuthors SET NOCOUNT OFF -- turn back on record counts RETURN @iPageCount END
Recordset Paging • <HTML><HEAD><TITLE>SQL Paging</TITLE></HEAD> • <BODY> • <% • Dim rsData • Dim intPage • Dim intLastPage • Dim fldF • Dim cmdAuthors • Dim strQuote • Dim strScriptName • Dim strConn • strConn="Provider=SQLOLEDB;Data Source=Hogwartz;Initial Catalog=pubs;User ID=sa; Password=letmein" • strQuote = Chr(34) 'the double quote character • If Request.QueryString("PAGE")="" Then • intPage=1 • Else • intPage = CInt( Request.QueryString("PAGE") ) • If intPage < 1 then • intPage=1 • End If • End If • Set cmdAuthors = Server.CreateObject("ADODB.Command") • Set rsData = Server.CreateObject("ADODB.Recordset")
With cmdAuthors • .ActiveConnection = strConn • .CommandText = "usp_PagedAuthors" • .CommandType = 4 'adCmdStoredProc=4 • .Parameters.Append .CreateParameter("RETURN_VAL", 3, 4) 'adInteger=3 adParamReturnValue=4 • .Parameters.Append .CreateParameter("@iPage", 3, 1, 8, intPage) 'adInteger=3 adParamInput=1 • .Parameters.Append .CreateParameter("@iPageSize", 3, 1, 8, 10) 'adInteger=3 adParamInput=1 • Set rsData = .Execute • .Execute • intLastPage = .Parameters("RETURN_VAL") • End WIth • Response.Write "Page = " & intPage & "<BR>“ • Response.Write "<TABLE BORDER=1><THEAD><TR>" • For Each fldF In rsData.Fields • Response.Write "<TD>" & fldF.Name & "</TD>" • Next • Response.Write "</TR></THEAD><TBODY>" • For intRec = 1 to rsData.PageSize • If Not rsData.EOF Then • Response.Write "<TR>" • For Each fldF In rsData.Fields • Response.Write "<TD>" & fldF.Value & "</TD>" • Next • Response.Write "</TR>" • rsData.MoveNext • End If • Next • Response.Write "</TBODY></TABLE></P>"
strScriptName = Request.ServerVariables("SCRIPT_NAME") • Response.Write " <A HREF=" & strQuote & strScriptName & "?PAGE=1" & strQuote & "> First Page </A>" • 'Only give an active previous page if there are previous page • If intPage= 1 Then • Response.Write " <SPAN>Previous Page</SPAN>" • Else • Response.Write " <A HREF=" & strQuote & strScriptName & "?PAGE="& intPage - 1 & strQuote & "> Previous Page </A>" • End If • 'Only give an active next page if there are more pages • If intPage = intLastPage Then • Response.Write " <SPAN>Next Page</SPAN>" • Else • Response.Write " <A HREF=" & strQuote & strScriptName & "?PAGE="& intPage + 1 & strQuote & "> Next Page </A>" • End If • Response.Write " <A HREF=" & strQuote & strScriptName & "?PAGE=" & intLastPage & strQuote & "> LastPage </A>" • rsData.Close • Set rsData =Nothing • Set cmdAuthors = Nothing • %> • </BODY> • </HTML>