Click to See Complete Forum and Search --> : date validation


meenakshi_joshi09
February 10th, 2006, 02:57 AM
hi,
i am trying to insert date in database through a variable 'date1'. i have a textbox 'txtdateofbirth' and using following code to convert value of textbox
in correct date format :
DateTime date1;
date1 = Convert.ToDateTime(txtDateOfBirth.Text);
it's working nicely, but in database time as 12:00:00 AM is also inserting with date (for ex. 2/8/1983 12:00:00 AM). i do not want to insert time with date.

can anybody solve my problem?

ITGURU
February 10th, 2006, 03:13 AM
Dear meenakshi,

This is default for storing Date field in Database, you cannot do anything on this. Only you can do is while displaying the Date in your Page, just remove the Time from the variable in which you received the value of the Date field or Use the Convert function of the SQL Server with Date field while writing the Query for fetching the Data from SQL Server.

mmetzger
February 10th, 2006, 10:05 AM
If you set the datatype in the table to smalldatetime, you can remove the time component.

meenakshi_joshi09
February 15th, 2006, 02:38 AM
thanks to ITGURU and mmetzger for their reply, but my problem was something else. i did not have a problem of datatype in table and did not have a problem in retriving date. i have a problem while inserting date in database, that is time was also inserting with date. now i have solve my problem by using following code while inserting data in database :

DateTime date1;
date1 = Convert.ToDateTime(txtDateOfBirth.Text);
//

code

//
string DOB=date1.Date.ToShortDateString();
string query="insert into SSAdmin(dateofbirth)values('"+DOB+"')";

now it's working properly.