Documente Academic
Documente Profesional
Documente Cultură
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.
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...
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.
<html>
<body>
<%
'' 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()
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...
<%
'' 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)
</table>
</body>
</html>
Visual Basic (VB) .NET aspx code
(highlighted with red) used to list database records
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>
</body>
</html>
HTML code used to input a database record
<html>
<body>
<%
'' 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")
</body>
</html>
Visual Basic (VB) .NET aspx code
(highlighted with red) used to add a record to the database
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>
</body>
</html>
HTML code used to delete a database record
<html>
<body>
<%
Dim id2 as String = Request.form("id")
</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