Web Q&A: Switch Focus Between Frames, Connect a...

We were unable to locate this content in de-de.

Here is the same content in en-us.

From the June 2000 issue of MSDN Magazine.
This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links. MIND
Web Q&A
Switch Focus Between Frames, Connect a Web Page to a Database
Robert Hess

Q When I select an item in one listbox, I want it to filter the options in another listbox. For example, I have a listbox that contains office locations. When I select a location, a second listbox should display the servers at that particular location. How do I update the second listbox based on the choice from the first listbox?

A Figure 1 shows one way to accomplish this. In this sample code, I use the remove and add methods of a select list to update the contents dynamically. Since add is a method of a Select object, you must add Option objects to it. That's why you see this in my code:



element = document.createElement("OPTION");
element.text = products[i];
document.Microsoft.product.add (element);
      Note that this code uses the Dynamic HTML capabilities of Internet Explorer 4.0 and higher, so it won't work in Netscape Navigator or earlier versions of Internet Explorer. To do something similar in Navigator, you'd need to maintain multiple layers in which the lists are organized. So, to arrive at a global solution, you should determine how important cross-browser compatibility is, how important it is to do everything on the client, and if you want to use browser detection so you can return the proper page to the user.
      You could also use frames for a robust and compatible solution. Clicking on a list item that was displayed in one frame would issue a server request to load the appropriate list into the next frame.

Q How can I connect a database to a Web page? Do I need some special software or can VBScript be used to interface with a database?

A Connecting a database to a Web page can be easy, as long as you are running the right software. First you must be running an ODBC-compliant database. Second, you need some server-side scripting available.
      A tutorial on integrating a database into your Web site is beyond the scope of this column, but you can check out http://www.microsoft.com/data/ado/sams/ch08.htm . This is a chapter from the book, Teach Yourself Active Web Database Programming in 21 Days (SAMS, 1997). It explains the advantages of ActiveX Data Objects (ADO) for database access, reviews the objects in the ADO model, and describes the differences between ADO, DAO (Data Access Objects), and RDO (Remote Data Objects). You can also find technical information at http://www.microsoft.com/data/techmat.htm.
      Many Web page authoring tools such as Microsoft® FrontPage® and Visual InterDev® include support for adding database functionality to your Web page. Check the authoring tool you are using to see how it exposes the interface for database operations. Web authoring tools often add lots of code that can be difficult to wade through, but I highly recommend looking at all the code anyway. Messing around with it to discover how database interaction is accomplished is a valuable exercise. And as long as the right statements are executed in the right order, connectivity can be straightforward.
      Here is some sample ASP code that I use for interacting with a database. Assuming you have the name of the database file stored in the path variable, the following code will create an ADO connection:


<%
    DB = "DBQ=" & path & ";Driver=
         {Microsoft Access Driver (*.mdb)};"
    Set Conn = Server.CreateObject("ADODB.Connection")
    Conn.Open DB
%>
Once the database is open, the following snippet will show you a list of tables:

<%
    adSchemaTables = 20
    Set RS_item = Conn.OpenSchema(adSchemaTables)
    Do While Not RS_item.EOF
        if (NOT StrComp (RS_item("TABLE_TYPE"), "TABLE")) then
%>
<%=RS_item("TABLE_NAME")%><br>
<%
        end if
        RS_item.MoveNext
    Loop
    RS_item.Close
%>
      The most interesting thing to do with a database is query it. Here, I assume that you have a connection open, and that the variable dbTable contains the name of the table you want to examine. This query essentially looks at all of the data in the table and displays it on the page:


<%
Query = "SELECT * FROM " & dbTable
Response.write ("Query = " & Query & "<br>")
Set RS_item = Conn.Execute(Query)
Do While Not RS_item.EOF
    for each field in RS_item.fields
%>
<b><%=field.name%></b> = "<%=field.value%>"<br>
<%
    next
    RS_item.MoveNext
Loop
%>
      Make sure that you close the connection as soon as you are finished using it:


<%
Conn.Close
%>
      There are many ways to interact with a database; some are server based and some are client based. Client-based solutions often use script code on the Web page, though some use ActiveX controls or Java-language applets. What you decide to use will depend on the abilities and restrictions that your servers impose on you.

Robert Hess is an engineer in the Microsoft Developer Relations group. He provides ISVs with information to help them develop apps for Microsoft systems software. Send e-mail to webqa@microsoft.com.

Page view tracker