Hey lisamaclea,
Please refer below sample
HTML
Search Customer:
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button Text="Search" runat="server" OnClick="Search" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPaging">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.SearchCustomers();
}
}
private void SearchCustomers()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
string sql = "SELECT CustomerId, Name, Country FROM Customers";
if (!string.IsNullOrEmpty(txtSearch.Text.Trim()))
{
sql += " WHERE REPLACE(REPLACE(REPLACE(REPLACE(Name,'.',''),'_',''),'-',''),'&',',') LIKE '%'+REPLACE(REPLACE(REPLACE(REPLACE(@ContactName,'.',''),'_',''),'-',''),'&',',') + '%'";
cmd.Parameters.AddWithValue("@ContactName", txtSearch.Text.Trim());
}
cmd.CommandText = sql;
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
protected void Search(object sender, EventArgs e)
{
this.SearchCustomers();
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
this.SearchCustomers();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.SearchCustomers()
End If
End Sub
Private Sub SearchCustomers()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand()
Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers"
If Not String.IsNullOrEmpty(txtSearch.Text.Trim()) Then
sql += " WHERE REPLACE(REPLACE(REPLACE(REPLACE(Name,'.',''),'_',''),'-',''),'&',',') LIKE '%'+REPLACE(REPLACE(REPLACE(REPLACE(@ContactName,'.',''),'_',''),'-',''),'&',',') + '%'"
cmd.Parameters.AddWithValue("@ContactName", txtSearch.Text.Trim())
End If
cmd.CommandText = sql
cmd.Connection = con
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub Search(sender As Object, e As EventArgs)
Me.SearchCustomers()
End Sub
Protected Sub OnPaging(sender As Object, e As GridViewPageEventArgs)
gvCustomers.PageIndex = e.NewPageIndex
Me.SearchCustomers()
End Sub
Screenshot
