connectseekers.com

Friday, 7 November 2014

Search and Display Data In GridView From Database Table In Asp.Net Using C#.Net

11/07/2014 - By Pranav Singh 1

This article will show you how you can Search and Display Data In GridView From Database Table In Asp.Net Using C#.Net. In this you will perform search operation on the bases of added textbox value.

So for this article first we will create a new sql table in which we will add some data.


 Now add some value in table.


Here is the table creation query.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[UserDetail](
      [Id] [int] NULL,
      [Name] [varchar](50) NULL,
      [EmailId] [varchar](50) NULL,
      [Address] [varchar](100) NULL,
      [Status] [int] NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

Now in this we will create a new asp.net application and add the below code into the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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 and Display Data In GridView From Database Table In Asp.Net Using C#.Net
    </title>
    <style type="text/css">
        .style1
        {
            height: 41px;
        }
        .style2
        {
            height: 41px;
            width: 142px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="100%" cellpadding="5" cellspacing="5">
            <tr>
                <td align="right" class="style1">
                    Search</td>
                <td align="left" class="style2">
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
                <td align="left" class="style1">
                    <asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click"
                        Text="Search" />
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <asp:GridView ID="GridView1" runat="server" Width="100%">
                    </asp:GridView>
                </td>
              
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Now on button click add the below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView("");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGridView(TextBox1.Text);
        }
        private void BindGridView(string searchVal)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
            try
            {
                DataTable objdt = new DataTable();
                string query = searchVal == "" ? "select * from UserDetail;" : "select * from UserDetail where name like '%" + searchVal + "%';";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                con.Open();
                da.Fill(objdt);
                con.Close();
                if (objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = objdt;
                    GridView1.DataBind();
                }
            }
            catch
            {
                con.Close();
            }
        }
    }
}

In code I have created a new function and in this function I have passed the search value on the bases of search value query will get prepared and data will displayed on the grid view.

Now add the connection string in your web.config file.

<connectionStrings>
              <add name="con" connectionString="Data Source=ADMIN-PC;Initial Catalog=Test;uid=sa;pwd=ajay;"
          providerName="System.Data.SqlClient" />
       </connectionStrings>


Now we have done run the application and check the output.


Now add some value ad click on search.

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

1 comment:

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
Email Id : contact@aspdotnet-pools.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


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top