Hi Bhavesh23,
Check this example. Now please take its reference and correct your code.
HTML
<asp:TextBox ID="txtDob" runat="server" />
<asp:Button Text="Calculate" runat="server" OnClick="OnCalculate" />
<hr />
<asp:TextBox ID="txtAge" runat="server" />
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Globalization;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Globalization
Code
C#
protected void OnCalculate(object sender, EventArgs e)
{
DateTime birthDate = DateTime.ParseExact(txtDob.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
SqlCommand cmd = new SqlCommand(string.Format("SELECT DATEDIFF(YY,'{0}',GETDATE())", birthDate), con);
con.Open();
string year = Convert.ToString(cmd.ExecuteScalar());
con.Close();
txtAge.Text = year;
}
VB.Net
Protected Sub OnCalculate(ByVal sender As Object, ByVal e As EventArgs)
Dim birthDate As DateTime = DateTime.ParseExact(txtDob.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim cmd As SqlCommand = New SqlCommand(String.Format("SELECT DATEDIFF(YY,'{0}',GETDATE())", birthDate), con)
con.Open()
Dim year As String = Convert.ToString(cmd.ExecuteScalar())
con.Close()
txtAge.Text = year
End Sub
Screenshot