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.
Some of my previous articles are as follows:
GridView
Bind By Sql Table Using DataTable,SqlDataAdapter,SqlConnection In Asp.Net Using
C#, Highlight
GridView Row on Mouseover Using CSS in Asp.Net C#.Net, Code
to Select All Checkbox in GridView in Asp.Net Using jQuery, How
to Bind Data To TextBox Values in Asp.Net GridView Control Using C#.Net, Validate
Selection Of Checkbox In GirdView Using Asp.net C# and jQuery | Force User To
Select One CheckBox Of GridView Before Submit in Asp.net Using javascript, GridView
- Delete Selected Row Record From DataBase On Button Click in Asp.net Using
C#.Net.
So for this article first we will create a new sql table in
which we will add some data.
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.
thanks a lot
ReplyDeleteThanks for your valuable comment
DeleteI want to serach the whole table data from sql server and store into gridviw one by one each row how it is possible please help me.
ReplyDelete