Introduction
This is a very quick time application built using Microsoft ASP.NET. Its basic purpose is to show the power and Rapid Application Development features in ASP.NET.
Searching and record listing is a very common task in dynamic website development. In ASP, it can be done using HTML tables and looping with the help of "for
-next
" or "while
-wend
" loops using VBScript.
Code Example of showing grid record in ASP 3.0
<% dim rs
dim cn
dim msg
set cn = server.CreateObject("Adodb.Connection")
set rs = server.CreateObject("Adodb.Recordset")
cn.Open "Provider=Microsoft.Jet.oledb.4.0;" & _
" Data Source=C:\Inetpub\wwwroot\biblio\db\biblio.mdb"
rs.open "Select * From rec", cn%>
<p>
<%
response.Write(msg)
if not rs.EOF then
%>
</p>
<form name="form1" method="post" action="">
<table width="44%" border=1 cellpadding=1
cellspacing=1 bordercolor="#000000">
<tr bgcolor="#CCCCCC">
<td width="4%"> </td>
<td width="96%"><strong>Name</strong></td>
</tr>
<%while not rs.EOF
n = n + 1
%>
<tr>
<td width="4%"> <input id=checkbox1 type=checkbox
name=d<%=n%> value="<%=rs.Fields("id")%>">
</td>
<td><a href="new.asp?id=<%=rs.Fields("ID")%>">
<%=rs.Fields("Name")%></a></td>
</tr>
<%
rs.MoveNext
wend
%>
<tr>
<td colspan="2"> <input type="hidden" name="num" value="<%=n%>">
<input id=submit type=submit value=Delete name=submit> </td>
</tr>
</table>
</form>
<p>
<%
else
Response.Write "No record found"
end if
rs.Close
cn.Close %>
This was the normal style used by me for common searching and listing type functions. And I have to copy and paste that code for all forms and listing options.
Plus in ASP 3.0, there was no option to properly reuse code. Normally, reuse was for just basic components like header, footer, and menu bar, but reusing source code and dynamic contents is difficult.
Code Using ASP.NET
Me.OleDbConnection1.ConnectionString = _
"Provider=Microsoft.Jet.oledb.4.0;" & _
" Data Source=C:\Inetpub\wwwroot\biblio\db\biblio.mdb"
Me.OleDbConnection1.Open()
Me.OleDbDataAdapter1.SelectCommand.CommandText = _
"Select ISBN,Title from Titles where title like'%" _
& TextBox1.Text & "%'"
Me.OleDbDataAdapter1.SelectCommand.Connection = _
Me.OleDbConnection1Me.OleDbDataAdapter1.Fill(Me.DataSet1)
Me.DataGrid1.DataSource = Me.DataSet1Me.DataGrid1.DataBind()
Label1.Text = "Total " & Me.DataSet1.Tables(0).Rows.Count _
& " record(s) found"
This is the total code that I had to write for this complete searching application. And Visual Studio simplifies the process of adding user controls and changing its properties.
It's a Visual Basic 6 like experience for building web applications.
Features
This small application has the following features:
- Database connectivity to MS Access database using ADO.NET.
- Webform controls.
- Use of
RequiredFieldValidator
control.
- Query execution for MS Access database using
Command
object.
- Search record listing option using
DataGrid
control with paging option.
About ASP.NET
ASP.NET is a great tool for building web applications. It has great support for new standard programming platforms like XML, Web Services, HTTP.
But most exciting feature are user controls that are pre-made objects and very easy to reuse in same old fashioned Visual Basic style.
Plus developers have option to create their own user controls, or customize and subclass standard controls, and makes repetitive jobs a lot easily and less time consuming.