Hi sambath,
Please refer below sample.
Database
For this sample I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<div>
Name:
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
<asp:GridView ID="gvCustomers" runat="server" AllowPaging="true" AutoGenerateColumns="false"
OnPageIndexChanging="OnPageIndexChanging" PageSize="5">
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" PreviousPageText="Previous"
NextPageText="Next" LastPageText="Last" />
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</div>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerID, ContactName, City, Country FROM Customers WHERE Country = @Country OR @Country IS NULL", con))
{
if (!string.IsNullOrEmpty(txtCountry.Text.Trim()))
{
cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim());
}
else
{
cmd.Parameters.AddWithValue("@Country", DBNull.Value);
}
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
this.BindGrid();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
this.BindGrid();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerID, ContactName, City, Country FROM Customers WHERE Country = @Country OR @Country IS NULL", con)
If Not String.IsNullOrEmpty(txtCountry.Text.Trim()) Then
cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim())
Else
cmd.Parameters.AddWithValue("@Country", DBNull.Value)
End If
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvCustomers.PageIndex = e.NewPageIndex
Me.BindGrid()
End Sub
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.BindGrid()
End Sub
Screenshot