Hi tjbowesfolio,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
Search Customer:
<asp:TextBox ID="txtSearch" runat="server" TextMode="MultiLine" Height="100px"></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="CustomerID" HeaderText="Id" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.SearchCustomers();
}
}
protected void Search(object sender, EventArgs e)
{
this.SearchCustomers();
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
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, ContactName, City, Country FROM Customers";
if (!string.IsNullOrEmpty(txtSearch.Text.Trim()))
{
List<string> customerIds = new List<string>();
char[] characters = { ',', '\n' };
string[] ids = txtSearch.Text.Trim().Split(characters, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < ids.Length; i++)
{
customerIds.Add(ids[i].Trim());
}
sql += " WHERE CustomerID IN ('" + string.Join("','", customerIds.ToArray()) + "')";
}
cmd.CommandText = sql;
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
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
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
Private Sub SearchCustomers()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
Dim sql As String = "SELECT CustomerId, ContactName, City, Country FROM Customers"
If Not String.IsNullOrEmpty(txtSearch.Text.Trim()) Then
Dim customerIds As List(Of String) = New List(Of String)()
Dim characters As Char() = {","c, vbLf}
Dim ids As String() = txtSearch.Text.Trim().Split(characters, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 0 To ids.Length - 1
customerIds.Add(ids(i).Trim())
Next
sql += " WHERE CustomerID IN ('" & String.Join("','", customerIds.ToArray()) & "')"
End If
cmd.CommandText = sql
cmd.Connection = con
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Screenshot