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
<asp:TextBox ID="txtSearch" runat="server" TextMode="MultiLine" Height="100px"></asp:TextBox><br />
<asp:Button Text="Search" runat="server" OnClick="Search" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="true" AllowPaging="true"
OnPageIndexChanging="OnPaging">
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
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()
{
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++)
{
if (!string.IsNullOrEmpty(ids[i].Trim()))
{
if (!IdExist(ids[i].Trim()))
{
customerIds.Add(ids[i].Trim() + " — allowed");
}
else
{
customerIds.Add(ids[i].Trim() + " — exluded");
}
}
}
gvCustomers.DataSource = customerIds;
gvCustomers.DataBind();
}
}
private bool IdExist(string id)
{
bool isExist = false;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT CustomerId FROM Customers WHERE CustomerId = @Id";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
isExist = Convert.ToString(cmd.ExecuteScalar()) == "" ? true : false;
con.Close();
}
}
return isExist;
}
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()
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
If Not String.IsNullOrEmpty(ids(i).Trim()) Then
If Not IdExist(ids(i).Trim()) Then
customerIds.Add(ids(i).Trim() & " — allowed")
Else
customerIds.Add(ids(i).Trim() & " — exluded")
End If
End If
Next
gvCustomers.DataSource = customerIds
gvCustomers.DataBind()
End If
End Sub
Private Function IdExist(ByVal id As String) As Boolean
Dim isExist As Boolean = False
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT CustomerId FROM Customers WHERE CustomerId = @Id"
cmd.Connection = con
cmd.Parameters.AddWithValue("@Id", id)
con.Open()
isExist = If(Convert.ToString(cmd.ExecuteScalar()) = "", True, False)
con.Close()
End Using
End Using
Return isExist
End Function
Screenshot
![](https://i.imgur.com/HJkzC2E.jpg)