Hi ashish007,
Check below sample.
HTML
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2"
HorizontalAlign="Center" RepeatDirection="Horizontal" CellSpacing="3">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
<asp:Label runat="server" Text='<%#Eval("LastName") %>'></asp:Label>
<br /><br />
<asp:Label ID="lblbday" runat="server" Text="Birthday" Font-Bold="true" ForeColor="Green"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindData();
}
}
private void BindData()
{
DataSet odt = new DataSet();
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Id"), new DataColumn("FirstName"),
new DataColumn("LastName"), new DataColumn("D_O_B") });
dt.Rows.Add(1, "Ram", "Sharma", "17/11/1984");
dt.Rows.Add(2, "Ashish", "Khan", "23/11/1984");
odt.Tables.Add(dt);
if (odt.Tables[0].Rows.Count > 0)
{
DataList1.DataSource = odt;
DataList1.DataBind();
for (int i = 0; i < odt.Tables[0].Rows.Count; i++)
{
string db = odt.Tables[0].Rows[i]["D_O_B"].ToString();
string[] strarr = db.Split('/');
int day = Int32.Parse(strarr[0]);
int month = Int32.Parse(strarr[1]);
int year = Int32.Parse(strarr[2]);
string someDate = Convert.ToString(year + "/" + month + "/" + day);
DateTime startDate = DateTime.Parse(someDate);
DateTime now = DateTime.Now;
if (startDate.Month == now.Month)
{
int birthDate = startDate.Day;
int todayDate = now.Day;
if (birthDate > todayDate)
{
DataListItem item = DataList1.Items[i];
Label lblday = item.FindControl("lblbday") as Label;
lblday.Text = "Birth Day come in " + (birthDate - todayDate).ToString() + " days.";
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindData()
End If
End Sub
Private Sub BindData()
Dim odt As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Id"), New DataColumn("FirstName"),
New DataColumn("LastName"), New DataColumn("D_O_B")})
dt.Rows.Add(1, "Ram", "Sharma", "17/11/1984")
dt.Rows.Add(2, "Ashish", "Khan", "23/11/1984")
odt.Tables.Add(dt)
If odt.Tables(0).Rows.Count > 0 Then
DataList1.DataSource = odt
DataList1.DataBind()
For i As Integer = 0 To odt.Tables(0).Rows.Count - 1
Dim db As String = odt.Tables(0).Rows(i)("D_O_B").ToString()
Dim strarr As String() = db.Split("/"c)
Dim day As Integer = Int32.Parse(strarr(0))
Dim month As Integer = Int32.Parse(strarr(1))
Dim year As Integer = Int32.Parse(strarr(2))
Dim someDate As String = Convert.ToString(year & "/" & month & "/" & day)
Dim startDate As DateTime = DateTime.Parse(someDate)
Dim now As DateTime = DateTime.Now
If startDate.Month = now.Month Then
Dim birthDate As Integer = startDate.Day
Dim todayDate As Integer = now.Day
If birthDate > todayDate Then
Dim item As DataListItem = DataList1.Items(i)
Dim lblday As Label = TryCast(item.FindControl("lblbday"), Label)
lblday.Text = "Birth Day come in " & (birthDate - todayDate).ToString() & " days."
End If
End If
Next
End If
End Sub
Screenshot