Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET Table Searching

0.00/5 (No votes)
16 Jun 2004 1  
Searching database and showing results in DataGrid control.

Sample screenshot

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:

  1. Database connectivity to MS Access database using ADO.NET.
  2. Webform controls.
  3. Use of RequiredFieldValidator control.
  4. Query execution for MS Access database using Command object.
  5. 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here