Hi nauna,
Refer below sample.
HTML
CustomerId :
<asp:TextBox runat="server" ID="txtId" />
<br />
<asp:Button Text="Display" runat="server" OnClick="Display" />
<br />
<asp:GridView runat="server" ID="gvCustomers" 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.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
UserBO.cs
public class UserBO
{
private int _CustomerId;
private string _Name;
private string _Country;
public int CustomerId
{
get
{
return _CustomerId;
}
set
{
_CustomerId = value;
}
}
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public string Country
{
get
{
return _Country;
}
set
{
_Country = value;
}
}
}
CS.aspx.cs
protected void Display(object sender, EventArgs e)
{
gvCustomers.DataSource = selectallproject();
gvCustomers.DataBind();
}
public List<UserBO> selectallproject()
{
List<UserBO> returnlis = new List<UserBO>();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE CustomerId = @CustomerId", con))
{
cmd.Parameters.AddWithValue("@CustomerId", txtId.Text);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
returnlis.Add(new UserBO { CustomerId = Convert.ToInt16(sdr["CustomerId"]), Name = sdr["Name"].ToString(), Country = sdr["Country"].ToString() });
}
con.Close();
}
}
return returnlis;
}
UserBOVB.cs
Public Class UserBOVB
Private _CustomerId As Integer
Private _Name As String
Private _Country As String
Public Property CustomerId As Integer
Get
Return _CustomerId
End Get
Set(ByVal value As Integer)
_CustomerId = value
End Set
End Property
Public Property Name As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property Country As String
Get
Return _Country
End Get
Set(ByVal value As String)
_Country = value
End Set
End Property
End Class
VB.aspx.vb
Protected Sub Display(ByVal sender As Object, ByVal e As EventArgs)
gvCustomers.DataSource = selectallproject()
gvCustomers.DataBind()
End Sub
Public Function selectallproject() As List(Of UserBOVB)
Dim returnlis As List(Of UserBOVB) = New List(Of UserBOVB)()
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 = @CustomerId", con)
cmd.Parameters.AddWithValue("@CustomerId", txtId.Text)
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
returnlis.Add(New UserBOVB With {
.CustomerId = Convert.ToInt16(sdr("CustomerId")),
.Name = sdr("Name").ToString(),
.Country = sdr("Country").ToString()
})
End While
con.Close()
End Using
End Using
Return returnlis
End Function
Screenshot