arpitsharma

how to retrieve only date from datetime from database

2 posts in this topic

i have a form in which i have a field date of birth , and a textbox to fill date of birth , when a person fills his date of birth like 01/01/1990 , then it will be saved into database , and when i retrieve this date form database then this comes 01/01/1990 12:00 AM , i want only date part from it.

This is my form to fillup date of birth

form.aspx

 <tr>
      <td width="30%">
         Date of Birth
          <br />
      </td>   <td style="width: 1px">
       :<asp:TextBox ID="txtDateofBirth" runat="server" placeholder="dd/mm/yyyy"></asp:TextBox>
       </td> <td class="td2">                                                                                     
                                                                 
                                                                                               
          </td>
          </tr>

this is code for form.aspx.cs

 SqlCommand cmd = new SqlCommand("insert into ApplicantForm(NameofApplicant,  dateofbirth)" +
                                                                "values( @NameofApplicant, convert(Datetime, @dateofBirth, 103)", con);
cmd.Parameters.AddWithValue("@dateofBirth",txtDateofBirth.Text);

this is code for print of this date of birth

print.aspx

   <asp:Label ID="txtDOB" runat="server"  ></asp:Label>

this is code print.aspx.cs

  SqlCommand cmd = new SqlCommand("Select * from ApplicantForm Where ApplicantId=" + ((Request.QueryString["ApplicantId"])) + "", con);
 dr = cmd.ExecuteReader();
 
txtDOB.Text = dr["dob"].ToString();

 

Share this post


Link to post
Share on other sites

Why not just use a date field instead of datetime? Otherwise, I'm sure there are some SQL functions you can use to return only the date portion of a datetime field.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now