Search Data in ASP.NET Web App and get the results.

Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 5356 red flag

This article explains on searching in website and get the results and bind in gridview.


Hello DNF Members, today we will be looking into very simple but very useful concept of search in,where the data comes from the database on the base of the search criteria.This is very helpful to requirement of developing admin panel for website


To make search easy in with Interaction of the database.

Using the code

We use  one textbox where input is entered as the search criteria.If the search criteria matches the value in the database results will be displayed in gridview.

In this article, we have one table Category and searching the category by entering the first two alphabets.

One stored procedure where the procedure requires one parameter i.e. input for search criteria and that will get the data from the Table of Category.

Block of code should be set style as "Code" like below.
//Script for Database
create database SFW
// Script for Table
create table CategoryMaster
CategoryID int identity(1,1)
CategoryName varchar(50),
Rowdeleted tinyint
// Script for Stored Procedure
ALTER procedure [dbo].[GetCategories4Autocomp]
@input varchar(250)
Select distinct top 10 CategoryName from CategoriesMaster 
where CategoryName like @input + '%' and RowDeleted = 0
//HTML Mark UP
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <div align="center">
        Enter Search Criteria : &nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="xtxtSearch" runat="server"></asp:TextBox> <br /><br />
        <asp:Button ID="btnSearch" runat="server" Text="Search" 
            onclick="btnSearch_Click" />
        <br />
        <br />
         <br />
        <br />
    <asp:GridView ID="xgvDetails" runat="server" AutoGenerateColumns="False">
            <asp:BoundField DataField="CategoryName" HeaderText="Category Name" />
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class Search : System.Web.UI.Page
    string strconn = ConfigurationManager.ConnectionStrings["cnnlocal"].ToString();
    SqlConnection sqlcon = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)


    private void SearchData()
        sqlcon = new SqlConnection(strconn);
        cmd = new SqlCommand("GetCategories4Autocomp", sqlcon); // calling the stored procedure
        cmd.CommandType = CommandType.StoredProcedure;
        da = new SqlDataAdapter(cmd);
        da.SelectCommand.Parameters.AddWithValue("@input", SqlDbType.VarChar).Value = xtxtSearch.Text; // passing parameter to the procedure to the database
        if (ds.Tables[0].Rows.Count > 0)
            xgvDetails.DataSource = ds;
    protected void btnSearch_Click(object sender, EventArgs e)
Search Page

Results Fetched.


Hope this will get basic functionality working and it will help in application.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)