Sunteți pe pagina 1din 5

Multimedia eBook: Lab: ASP.NET with VB http://www.ecourses.ou.edu/cgi-bin/ebook.cgi?doc=&topic=mm&chap_s...

Ch 12. Server Scripts Multimedia Engineering Multimedia


Lab: ASP.NET
Introduction Perl ASP.NET SQL Flash
with VB
Multimedia Lab: ASP.NET with VB
Chapter
1. Introduction
2. Web Pages ASP.NET using Visual Basic (VB): Lab
3. Graphics
4. Drawing Click to Download zipped files
5. Sound
Introduction
6. 2D Animation
7. 3D Animation To better understand how ASP.NET (Active Server
8. Video Pages) can be used to deliver dynamic web content,
9. 3D Simulation
10. Data Visualiz.
this lab will walk you through the development of a
11. Browser Scripts simple Internet part database. The idea is to allow a
12. Server Scripts user to list, add and delete parts from a database
13. Collaboration through a web page. The database is on the web
server. This is a common task with commercial web
Appendix
sites. For example, at Amazon.com, when you request
a book, that information is pull from a database and
Search
place on the web page. This lab does the same thing,
eBooks
but with a very simple part database.
Dynamics
Fluids The lab will show how to use asp.net scripts (VB or
Math Listing of the database contents visual basic) to interact with an Microsoft Access
Mechanics database. Also, basic SQL (Structured Query
Multimedia
Mems
Language) commands will be used to pull and save
Statics data to the database. Since ASP.NET is just a
Thermodynamics framework, other languages, such as C# and C could
also be used in place of VB.
Author(s):
Kurt Gramoll
Files used in lab
©Kurt Gramoll
The lab will involve three aspx files, one each for the
three different functions. These three functions could
be placed in one aspx file, but it is easier to
understand the scripts if each task is in its own file.

File structure for lab


In addition to the aspx scripts, there are three html
files. The home.htm simply lets the user choose which
task to do. There will be three links on the page.

Lab Example The add and delete task needs user input (what to add
and which part to delete) so a html page with user
input fields is needed. However, the list task does not
need user input so there is no html file.

Create Database
Accessing a database is central to this lab so it should
be created first. ASP.NET can add and delete records
in a database, but the initial database file must be
created outside the web server. This means, just open
Access database program and set up a database with
at least one record. There is a database, called
Parts.mdb, included in the files that are downloaded.

Basic Access Our database will have three fields, name, id, and
table structure with price, as shown in the image on the right. Again, this is
three fields and 2 records a very simple database and asp.net can interact with
(table name is "Table1") much more complex databases.

In this lab, "name" is the description, "id" is the part


number will be the key field, and "price" is the cost of
Note: Folder Permission the part. All three fields are simple text fields in the
database, but could be number or currency fields if

1 of 5 11/4/2010 9:44 PM
Multimedia eBook: Lab: ASP.NET with VB http://www.ecourses.ou.edu/cgi-bin/ebook.cgi?doc=&topic=mm&chap_s...

desired. Specialized field types has advantages, but to


The directory or folder where the database file is keep this lab simple, only text fields are used.
placed must have write permission. Otherwise, it
cannot be modified by a web user. Both the name of the database and the name of the
table (Table1) can be anything but you need to
remember them since the names will be used to
access the fields.

List database content


One of the first tasks is to list the current content of
the database. There is no user input since all of the
records will be listed. In more complex systems, it is
possible to make partial listings, and then you would
need to ask the user what to list.

Note: Header Lines The first part of the code is to open a connection to
the database. First, a object needs to be creating an
At the start of any aspx file, there are various OleDbConnection object. The connection string needs
system information that needs to be set. First, the both the driver name ("Microsoft.Jet.OLEDB.4.0" for
language needs to be specified with Access) and the location of the database. For other
<%@ Page Language="VB" Debug="true" %>
databases (mySQL, SQL Server, etc. needs other
drivers). Next, you need to issue the open command.
Next, any ASP.NET resources and libraries need This only opens the communication channel to the
to be imported. For this application, the database.
system.data and system.data.OleDd is used.
After the connection is made, a SQL query command
<%@ Import Namespace="System.Data" %> must be sent to the data base. This is done by
<%@ Import Namespace="System.Data.OleDb" %> creating a OleDbCommand object using the connection
object previously created and the SQL statement.

Finally, the SQL command must be executed and the


results placed in a new OleDbDataReader object. This
data object is one of the simplest but their are others,
such as a DataSet object that could also be used.

Now the easy part, writing the information to the web


page. This code places the results in the
OleDbDataReader object into a HTML table structure.
The first part of the table is just HTML code. Then
there is a loop (While) to open and read each record
from the OleDbDataReader object. The results of this
loop are stored in a simple text variable that is then
written to the web page after all the data is read.
Note, the while loop structure does not need the
number of records to be known before the listing
starts.

<%@ Page Language="VB" Debug="true" %>


<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<html>
<body>

List Page - <a HREF="home.htm">top</a><p>

<%
'' Connect to database
Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\{path to file}\parts.mdb"
Dim Connect As OleDbConnection = New OleDbConnection(ConStr)
Connect.Open()

'' Set SQL command


Dim Query As String = "SELECT * FROM Table1"
Dim MyCommand As OleDbCommand = New OleDbCommand(Query, Connect)

'' Get data


Dim MyDataReader As OleDbDataReader = MyCommand.ExecuteReader()
%>

2 of 5 11/4/2010 9:44 PM
Multimedia eBook: Lab: ASP.NET with VB http://www.ecourses.ou.edu/cgi-bin/ebook.cgi?doc=&topic=mm&chap_s...

<!-- Table column headers -->


<table bgcolor="#CCFFFF" width="180" border="1" cellspacing="0" cellpadding="0">
<tr>
<td align=center> Name </td>
<td align=right> ID </td>
<td align=right> Price </td>
</tr>

<%
'' loop through data
Dim OutData As String
While MyDataReader.Read()
OutData &= "<tr>"
OutData &= "<td align=center> " & MyDataReader("name") & "</td>"
OutData &= "<td align=right> " & MyDataReader("id") & "</td>"
OutData &= "<td align=right> $" & MyDataReader("price") & "</td>"
OutData &= "</tr>"
End While
response.write(OutData)

'' disconnect from database


MyDataReader.Close()
Connect.Close()
%>

</table>
</body>
</html>
Visual Basic (VB) .NET aspx code
(highlighted with red) used to list database records

Add record to database


Another common task when dealing with
Internet-based databases is adding a new record
through a web page. The process is similar to listing of
a database, but it needs user input. The input is
supplied to the server through a HTML page using the
form tag.

The HTML code below has a basic form tag that is


used to submit user input data. The form tag must
have a "action" parameter which tells the server what
to execute when the form is received. The action is
usually a server-side script like aspx. Each of the input
values has its own name which will be used by the
aspx script to identify the variable. Finally, the form tag
must have a "submit" type input so the user has
something to click on. This input button tells the
browser that the user is down with the input values
and submit them to the server and execute the action.

After the HTML sends the data, the aspx script is


started and receives the data values. However, the
aspx script must explicitly ask for the data and assign
it to a variable within the aspx code. This is done with
a "Request.Form" command (see script below). For
each data value, a Request.Form must be executed.
Once the data is received, then the aspx code opens a
connection with the data base and sends it a SQL
command. The SQL command to add a record is
INSERT.

The INSERT command needs the exact number of


values that are in the actual table record. Too many or
too few will cause problems and errors. You will also
note that the execute command, ExecuteNonQuery(),
is different from the SELECT action. This is because
there is not data being pulled from the database, only
data being sent to the database. There is no need for

3 of 5 11/4/2010 9:44 PM
Multimedia eBook: Lab: ASP.NET with VB http://www.ecourses.ou.edu/cgi-bin/ebook.cgi?doc=&topic=mm&chap_s...

an OleDbDataReader object.

<html>
<body>

Add Page - <a HREF="home.htm">top</a><p>

<form method="post" action="add.aspx">


Part Name: <input type="text" size="20" name="name"><br>
Part ID: <input type="text" size="20" name="ic"><br>
Price: <input type="text" size="20" name="price"><p>

<input type="submit" name="submit" value="add">


</form>

</body>
</html>
HTML code used to input a database record

<%@ Page Language="VB" Debug="true" %>


<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<html>
<body>

Add Page - <a HREF="home.htm">top</a><p>

<%
'' Get inform from HTML page
Dim name2 as String = Request.form("name")
Dim id2 as String = Request.form("id")
Dim price2 as String = Request.form("price")

'' Connect to database


Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\{path to file}\parts.mdb"
Dim Connect As OleDbConnection = New OleDbConnection(ConStr)
Connect.Open()

'' Set SQL command


Dim Query As String = "INSERT INTO Table1 (name,id,price) VALUES ('" & name2 & "','" & id2 & "','" & price2 & "')"
Dim MyCommand As OleDbCommand = New OleDbCommand(Query, Connect)
MyCommand.ExecuteNonQuery()
Connect.Close() %>

The following part was added to the database:<br>


<br>
<table bgcolor="#FFFFCC" width="200" border="1" cellspacing="1" cellpadding="0">
<tr align=center> <td>Name</td><td><%=name2 %></td> </tr>
<tr align=center> <td>ID</td><td><%=id2 %></td> </tr>
<tr align=center> <td>Price</td><td><%=price2 %></td> </tr>
</table>

</body>
</html>
Visual Basic (VB) .NET aspx code
(highlighted with red) used to add a record to the database

Delete record to database


The last task is to delete a record from the database.
This like adding a record where the user must tell the
server what record needs to be deleted. This is done
with a standard HTML page using the form tag. Again,
you need a input tag to let the user type the part
number that will be deleted. There also needs to be a
submit button to send the form data to the server.

The server will start the asp code and then the asp
code will ask the server for the form value using a
Request.Form command. Again, a database
connection needs to set up and then an SQL command
is sent to the database. To delete a record, use the
DELETE command along with the record number to be

4 of 5 11/4/2010 9:44 PM
Multimedia eBook: Lab: ASP.NET with VB http://www.ecourses.ou.edu/cgi-bin/ebook.cgi?doc=&topic=mm&chap_s...

deleted.

<html>
<body>
Delete Page - <a HREF="home.htm">top</a>
<p>

<form method="post" action="delete.aspx">


Part: <input type="text" size="20" name="id"><br><p>
<input type="submit" name="submit" value="delete">
</form>

</body>
</html>
HTML code used to delete a database record

<%@ Page Language="VB" Debug="true" %>


<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<html>
<body>

Add Page - <a HREF="home.htm">top</a><p>

<%
Dim id2 as String = Request.form("id")

'' Connect to database


Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\{path to file}\parts.mdb"
Dim Connect As OleDbConnection = New OleDbConnection(ConStr)
Connect.Open()

'' Set SQL command


Dim Query As String = "DELETE FROM Table1 WHERE id = '" & id2 &"'"
Dim MyCommand As OleDbCommand = New OleDbCommand(Query, Connect)
MyCommand.ExecuteNonQuery()
Connect.Close() %>

The follwing part was deleted from the database:<br>


Number: <%=id2 %><br>

</body>
</html>
Visual Basic (VB) .NET aspx code
(highlighted with red) used to delete a record to the database

5 of 5 11/4/2010 9:44 PM

S-ar putea să vă placă și