I am saving users information in Sql Server 2005 Database. The problems are:
1. If a user selects 1/Jan/1990 then in the Age column of the database automatically 23 should be saved i.e users age, how do I calulate the age and use it in the query?
2. The format of DateofBirth column is incorrect it should be 1/Jan/1990 inspite of2013-01-01 00:00:00.000. The data type is Datetime
------------------Design is------------------------------------------------------------------------
<form id="form1" runat="server">
<div>
FirstName
<asp:TextBox ID="TxtFirstName" runat="server"></asp:TextBox>
<br />
<br />
LastName
<asp:TextBox ID="TxtLastName" runat="server"></asp:TextBox>
<br />
<br />
Gender
<asp:DropDownList ID="DdlGender" runat="server" Style="top: 105px; left: 80px;
position: absolute; height: 22px; width: 69px">
<asp:ListItem Value="-1">Select</asp:ListItem>
<asp:ListItem Value="1">Male</asp:ListItem>
<asp:ListItem Value="2">Female</asp:ListItem>
</asp:DropDownList>
<asp:Calendar ID="Calendar1" runat="server" Style="top: -1px; left: 260px; position: absolute;
height: 188px; width: 259px"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
</div>
<br />
DOB
<asp:TextBox ID="TxtDateofBirth" runat="server" Style="top: 143px; left: 66px; position: absolute;
height: 22px; width: 128px; right: 745px"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/calendar.png"
Style="top: 142px; position: absolute; width: 27px; left: 204px; height: 23px"
onclick="ImageButton1_Click" />
<br />
<br />
<asp:Button ID="BtnSubmit" runat="server" Text="Submit"
onclick="Button1_Click" />
</form>
---------------------------------------------------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Calendar1.Visible = false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String cn= ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
using(SqlConnection connection=new SqlConnection(cn))
{
SqlCommand command = new SqlCommand("insert into userdetails (FirstName, LastName, Gender, DateofBirth) values (@FirstName, @LastName, @Gender, @DateofBirth)", connection);
connection.Open();
command.Parameters.AddWithValue("@FirstName", TxtFirstName.Text);
command.Parameters.AddWithValue("@LastName", TxtLastName.Text);
command.Parameters.AddWithValue("@Gender", DdlGender.SelectedItem.Text);
command.Parameters.AddWithValue("@DateofBirth",TxtDateofBirth.Text);
command.ExecuteNonQuery();
}
}
protected void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if (Calendar1.Visible)
{
Calendar1.Visible = false;
}
else
{
Calendar1.Visible = true;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TxtDateofBirth.Text = Calendar1.SelectedDate.ToString("dd/MMM/yyyy");
Calendar1.Visible = false;
}