Hi Bhavesh23,
Please refer below Sample.
SQL
CREATE PROCEDURE Get_MonthNYear
@DateofBirth DATETIME
AS
BEGIN
DECLARE @CurrentDatetime DATETIME
DECLARE @Years VARCHAR(40)
DECLARE @Months VARCHAR(30)
SET @CurrentDatetime = GETDATE()
SELECT @Years = DATEDIFF(YEAR, @DateofBirth, @CurrentDatetime)
SELECT @Months = DATEDIFF(MONTH, @DateofBirth, @CurrentDatetime)-(DATEDIFF(YEAR, @DateofBirth, @CurrentDatetime)*12)
SELECT @Years +' years, ' + @Months +' months' AS YearNMonth
END
HTML
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox><br />
<asp:Button ID="btnCheck" runat="server" Text="Check" OnClick="OnCheck" /><br />
<asp:Label ID="lblData" runat="server"></asp:Label>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Globalization
Code
C#
protected void OnCheck(object sender, EventArgs e)
{
DateTime dob = DateTime.ParseExact(txtAge.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture);
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("Get_MonthNYear", con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@DateofBirth", dob);
string year = Convert.ToString(cmd.ExecuteScalar());
con.Close();
lblData.Text = year;
}
}
}
VB.Net
Protected Sub OnCheck(ByVal sender As Object, ByVal e As EventArgs)
Dim dob As DateTime = DateTime.ParseExact(txtAge.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture)
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("Get_MonthNYear", con)
con.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@DateofBirth", dob)
Dim year As String = Convert.ToString(cmd.ExecuteScalar())
con.Close()
lblData.Text = year
End Using
End Using
End Sub
Screenshot