Hi Sumeet,
Check this example. Now please take its reference and correct your code.
Create a class file and write your business logic and based on data you want to return from the class file assign the return type for the method.
HTML
<asp:Repeater ID="rptCustomers" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>
CustomerID
</th>
<th>
CompanyName
</th>
<th>
City
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("CustomerID") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblName" runat="server" Text='<%#Eval("CompanyName") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Code
BusinessData.cs
public SqlDataReader PopulateRepeater()
{
string str = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT TOP 5 CustomerID,CompanyName,City FROM Customers", conn);
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
BusinessData.vb
Public Function PopulateRepeater() As SqlDataReader
Dim str As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim conn As SqlConnection = New SqlConnection(str)
conn.Open()
Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 5 CustomerID,CompanyName,City FROM Customers", conn)
Dim dr As SqlDataReader = cmd.ExecuteReader()
Return dr
End Function
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BusinessData business = new BusinessData();
System.Data.SqlClient.SqlDataReader dr = business.PopulateRepeater();
this.rptCustomers.DataSource = dr;
this.rptCustomers.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim business As BusinessData = New BusinessData()
Dim dr As System.Data.SqlClient.SqlDataReader = business.PopulateRepeater()
Me.rptCustomers.DataSource = dr
Me.rptCustomers.DataBind()
End If
End Sub
Screenshot
