Hi SUJAYS,
Refer below sample.
HTML
Id :
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
<br />
Name :
<asp:TextBox runat="server" ID="txtName" />
<br />
<asp:Button ID="Button1" Text="Search" runat="server" OnClick="Search" />
<br />
<asp:GridView 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>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
BEL.cs
public class BEL
{
private int _Id;
public int id
{
get
{
return _Id;
}
set
{
_Id = value;
}
}
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
}
BLL.cs
public class BLL
{
public DataTable GetData(BEL enities)
{
DAL dal = new DAL();
return dal.Data(enities);
}
}
DAL.cs
public class DAL
{
public DataTable Data(BEL enities)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE CustomerId = @Id AND Name LIKE @Name + '%'", con))
{
cmd.Parameters.AddWithValue("@Id", enities.id);
cmd.Parameters.AddWithValue("@Name", enities.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)
{
if (!this.IsPostBack)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
}
}
}
protected void Search(object sender, EventArgs e)
{
BEL entity = new BEL();
entity.id = Convert.ToInt32(txtId.Text);
entity.Name = txtName.Text;
BLL bll = new BLL();
DataTable dt = bll.GetData(entity);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
VB.Net
BELVB.vb
Public Class BELVB
Private _Id As Integer
Public Property id As Integer
Get
Return _Id
End Get
Set(ByVal value As Integer)
_Id = value
End Set
End Property
Private _Name As String
Public Property Name As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
End Class
BLLVB.vb
Public Function GetData(ByVal enities As BELVB) As DataTable
Dim dal As DALVB = New DALVB()
Return dal.Data(enities)
End Function
DALVB.vb
Public Function Data(ByVal enities As BELVB) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers WHERE CustomerId = @Id AND Name LIKE @Name + '%'", con)
cmd.Parameters.AddWithValue("@Id", enities.id)
cmd.Parameters.AddWithValue("@Name", enities.Name)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Vb.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End If
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Dim entity As BELVB = New BELVB()
entity.id = Convert.ToInt32(txtId.Text)
entity.Name = txtName.Text
Dim bll As BLLVB = New BLLVB()
Dim dt As DataTable = bll.GetData(entity)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Screenshot