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

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

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

Introduction


Hello DNF Members, today we will be looking into very simple but very useful concept of search in asp.net,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

Objective

To make search easy in asp.net 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)
)
as
BEGIN
Select distinct top 10 CategoryName from CategoriesMaster 
where CategoryName like @input + '%' and RowDeleted = 0
END
//HTML Mark UP
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <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">
        <Columns>
            <asp:BoundField DataField="CategoryName" HeaderText="Category Name" />
        </Columns>
    </asp:GridView>
    </div>
    </form>
</body>
</html>
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
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            xgvDetails.DataSource = ds;
            xgvDetails.DataBind();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SearchData();
    }
}
Search Page



Results Fetched.




Conclusion


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

Raj.Trivedi
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
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
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 www.dotnetfunda.com 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 http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)