Monday, 15 September 2014

Search GridView Record on Button Click By Using C#.Net in Asp.Net

9/15/2014 - By Pranav Singh 3

This article will show you how you can search record preset in a gridview. In this user will add text in a TextBox and on click of button gridview data will get filtered using c#.net and asp.net.

So for this article first we will create a new asp.net article add ad the below tag into your asp.net page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProjectDemo_Asp.et.Default" %>

<!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>Search GridView Record on Button Click By Using C#.Net in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
   
    Search By Title : <asp:TextBox ID="txtsearch" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
        Text="Search" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no data records to display."
            Width="500px" BorderStyle="Solid" ShowFooter="True">
            <Columns>
                <asp:BoundField DataField="author_name" HeaderText="NAME" />
                <asp:BoundField DataField="publisher_name" HeaderText="PUB. NAME" />
                <asp:BoundField DataField="title" HeaderText="TITLE" />
                <asp:BoundField DataField="publication_year" HeaderText="PUB. YEAR" />
            </Columns>
            <HeaderStyle BackColor="#66CCFF" />
        </asp:GridView>
    </form>
</body>
</html>

Now just check the code on your .cs page.

using System;
using System.Data;
using System.Data.OleDb;
namespace ProjectDemo_Asp.et
{
    public partial class Default : System.Web.UI.Page
    {
        public string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\bookstore.mdb;Persist Security Info=False;";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable _objdt = new DataTable();
                _objdt = GetDataFromDataBase("");
                if (_objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = _objdt;
                    GridView1.DataBind();
                }
            }
        }

        ///
        /// Function for binding retribing the data from database
        /// In this i have used Access DB you can use SQL DB to bind the data
         ///
        public DataTable GetDataFromDataBase(string searchtext)
        {
            DataTable _objdt = new DataTable();
            string querystring = "";
            querystring = "select * from Books";
            if (querystring != "")
            {
                querystring += " where title like '%" + txtsearch.Text + "%';";
            }
            OleDbConnection _objcon = new OleDbConnection(connectionstring);
            OleDbDataAdapter _objda = new OleDbDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable _objdt = new DataTable();
            _objdt = GetDataFromDataBase(txtsearch.Text);
            if (_objdt.Rows.Count > 0)
            {
                GridView1.DataSource = _objdt;
                GridView1.DataBind();
            }
        }
    }
}
       
In above code I have create a function in which in which I have passed the search string as parameter. If the passed value if blank on that case I have fetched all the records other wise I have applied like query to search the record.

No we have some just run the page and check the output.


Now add some text and click on search button.

DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

3 comments:

  1. Thank you, I just needed something like this. Using vb.net it took some changing but I got it working. Nice

    ReplyDelete
    Replies
    1. Thanks for your valuable comment. keep visiting.

      Delete
  2. Thanku u sir, I just needed something like this.

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top