Hi ashish007,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2" HorizontalAlign="Center" RepeatDirection="Horizontal" CellSpacing="3">
<ItemTemplate>
<table>
<tr>
<td>
<asp:HiddenField ID="hfDOB" runat="server" Value='<%#Eval("BirthDate") %>' />
<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.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindData();
}
}
private void BindData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Employees";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
this.DataList1.DataSource = dt;
this.DataList1.DataBind();
foreach (DataListItem item in DataList1.Items)
{
Label lblday = item.FindControl("lblbday") as Label;
DateTime birthDate = Convert.ToDateTime((item.FindControl("hfDOB") as HiddenField).Value.Trim());
lblday.Text = this.ShowBirthDate(birthDate);
}
}
}
}
}
public string ShowBirthDate(DateTime startDate)
{
string remain = string.Empty;
DateTime now = DateTime.Now;
if (startDate.Month == now.Month)
{
int birthDate = startDate.Day;
int todayDate = now.Day;
if (birthDate > todayDate)
{
remain = "Birth Day come in " + (birthDate - todayDate).ToString() + " days.";
}
}
return remain;
}
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 conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM Employees"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Me.DataList1.DataSource = dt
Me.DataList1.DataBind()
For Each item As DataListItem In DataList1.Items
Dim lblday As Label = TryCast(item.FindControl("lblbday"), Label)
Dim birthDate As DateTime = Convert.ToDateTime((TryCast(item.FindControl("hfDOB"), HiddenField)).Value.Trim())
lblday.Text = Me.ShowBirthDate(birthDate)
Next
End Using
End Using
End Using
End Sub
Public Function ShowBirthDate(ByVal startDate As DateTime) As String
Dim remain As String = String.Empty
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
remain = "Birth Day come in " & (birthDate - todayDate).ToString() & " days."
End If
End If
Return remain
End Function
Screenshot