Click here to Skip to main content
15,891,567 members
Articles / Web Development / ASP.NET
Article

ASP.NET Table Searching

Rate me:
Please Sign up or sign in to vote.
1.73/5 (18 votes)
16 Jun 20042 min read 83.5K   1.9K   34   7
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

VBScript
<% 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

VB
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


Written By
Web Developer Source Systems
Pakistan Pakistan
Thanks for reading


Humayun Shabbir
http://enthusiastprogrammer.com

Comments and Discussions

 
Generalwack madarchood article Pin
Mogamboo_Khush_Hua9-Dec-09 23:03
Mogamboo_Khush_Hua9-Dec-09 23:03 
GeneralMy vote of 1 Pin
mmwlada20-May-09 0:39
professionalmmwlada20-May-09 0:39 
QuestionIs this the only reason 'Wackatronic' u got RED upon him? Pin
Asif Ashraf30-Sep-07 1:28
professionalAsif Ashraf30-Sep-07 1:28 
GeneralWTF Pin
Wackatronic17-Jun-04 15:28
Wackatronic17-Jun-04 15:28 
GeneralRe: WTF Pin
Humayun Shabbir17-Jun-04 22:03
Humayun Shabbir17-Jun-04 22:03 
GeneralRe: WTF Pin
Anonymous20-May-05 16:00
Anonymous20-May-05 16:00 
GeneralRe: WTF Pin
Humayun Shabbir15-Jul-05 1:44
Humayun Shabbir15-Jul-05 1:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.