Hi SUJAYS,
Please refer below sample.
HTML
Name :
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:GridView runat="server" ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" Text="Search" runat="server" OnClick="Search" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
DataClass.cs
public class DataClass
{
public DataTable GetData(string name)
{
string quer = "SELECT * FROM Customers WHERE Name LIKE @Name + '%'";
if (string.IsNullOrEmpty(name))
{
quer += " OR Name IS NOT NULL";
}
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(quer, con);
cmd.Parameters.AddWithValue("@Name", name);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
CS.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataClass dc = new DataClass();
DataTable dt = dc.GetData(txtName.Text);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
protected void Search(object sender, EventArgs e)
{
DataClass dc = new DataClass();
DataTable dt = dc.GetData(txtName.Text);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
VB.Net
DataClassVB.vb
Public Class DataClassVB
Public Function GetData(ByVal name As String) As DataTable
Dim quer As String = "SELECT * FROM Customers WHERE Name LIKE @Name + '%'"
If String.IsNullOrEmpty(name) Then
quer += " OR Name IS NOT NULL"
End If
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand(quer, con)
cmd.Parameters.AddWithValue("@Name", name)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Return dt
End Using
End Function
End Class
VB.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dc As DataClassVB = New DataClassVB()
Dim dt As DataTable = dc.GetData(txtName.Text)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Dim dc As DataClassVB = New DataClassVB()
Dim dt As DataTable = dc.GetData(txtName.Text)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Screenshot