Jump to content
arpitsharma

how to retrieve only date from datetime from database

Recommended Posts

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...