Hi Rezu2215,
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 runat="server" ID="txtName" />
<asp:Button Text="Search" runat="server" OnClick="Search" />
<br />
<br />
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<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>
<br />
<asp:Label ID="Label2" runat="server" />
Code
C#
protected void Search(object sender, EventArgs e)
{
NorthwindEntities entities = new NorthwindEntities();
var query = (from c in entities.Customers
where c.ContactName.Contains(txtName.Text)
select c).ToList();
if (query.Count > 0)
{
gvCustomers.DataSource = query;
gvCustomers.DataBind();
gvCustomers.Visible = true;
txtName.Text = "";
Label2.Text = "";
}
else
{
gvCustomers.Visible = false;
Label2.Visible = true;
Label2.Text = "No records available";
}
}
VB.Net
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Dim entities As NorthwindEntities = New NorthwindEntities()
Dim query = (From c In entities.Customers Where c.ContactName.Contains(txtName.Text) Select c).ToList()
If query.Count > 0 Then
gvCustomers.DataSource = query
gvCustomers.DataBind()
gvCustomers.Visible = True
txtName.Text = ""
Label2.Text = ""
Else
gvCustomers.Visible = False
Label2.Visible = True
Label2.Text = "No records available"
End If
End Sub
Screenshot
