Hey chetan,
Please refer below sample.
HTML
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="1">
<ItemTemplate>
<div style="border: 2px solid #ccc;">
<b>EventId:</b>
<asp:Label ID="Label1" Text='<%# Eval("EventId") %>' runat="server" />
<br />
<b>Event Name:</b>
<asp:Label ID="Label2" Text='<%# Eval("EventName") %>' runat="server" />
<br />
<b>Upcoming Event Date:</b>
<asp:Label ID="Label3" Text='<%#Eval("EventDate","{0:dd/MM/yyyy}") %>' runat="server" />
<br />
</div>
</ItemTemplate>
</asp:DataList>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindListView();
}
}
private void BindListView()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT EventId, EventName, EventDate FROM EventTest WHERE EventDate > = @EventDate";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@EventDate", DateTime.Now.ToString("yyy-MM-dd"));
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindListView()
End If
End Sub
Private Sub BindListView()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT EventId, EventName, EventDate FROM EventTest WHERE EventDate > = @EventDate"
cmd.Connection = con
cmd.Parameters.AddWithValue("@EventDate", DateTime.Now.AddYears(-60))
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
DataList1.DataSource = dt
DataList1.DataBind()
End Using
End Using
End Using
End Sub
Screenshot