Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

I am using datetime data type for a column and insert the value of a text box in it and i want to insert only date but in the table time also inserted with date

how can i insert only date
Posted

Hi dear,

string values = dateTimePicker1.Text;
 
Share this answer
 
Convert your text box value to date only:

string dValue = string.Format("{0:dd-MM-yyyy}",textbox1.Text);

Please also note that SQL Server by default stores time as well if the data type of the column is datetime. If you don't supply time, it will be defaulted to 00:00:00. If you change the column's data type to varchar or smalldatetime.
 
Share this answer
 
There are two option for doing this task

1. take varchar datatype of your table
2. take date datatype of your table

when you throw query from css than
string date=DateTime.now.tostring("here your date format like dd/MM/yyyy");

when you retrieving date from your database than

convert(date,your columnname,103)

where 103 is format of date
for more formats view

http://www.sql-server-helper.com/tips/date-formats.aspx[^]
 
Share this answer
 
if you don't need the time of the date field why don't you change the column data type as DATE?
then you can insert date in to column by using
Convert.ToDateTime(Textbox1.Text).Date;

If you can't change the data type of the column then you can get DATE from the DATETIME field when you read from the table.
 
Share this answer
 
try some thing like ...../.
Convert.ToDateTime(Textbox1.Text).Date;
 
Share this answer
 
Comments
Pradeep_kaushik 4-Dec-12 12:14pm    
DateTime dt = Convert.ToDateTime(TextBox5.Text).Date;

its not working
Convert.ToDateTime(textbox.text).Date
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900