to save the data to SQL database, but it saved as something like: 2012-10-24 00:00:00 000,
Now, I need to convert it to date only, so I changed the code as this:
If you are using SQL2008, change your column datatype to DATE.
I tried to change the Datetype to Date, but I can not find it from the dropdown list of datatype, I tried to manually change it to Date, it shows "Invalid date type".
Is anyway to use the C# code to convert if I can not change it this way? Thanks
Peter Cong
Member
527 Points
681 Posts
About using Convert.ToDateTime(StartDate.Text).ToShortDateString, help please,
Jan 02, 2013 06:44 PM|LINK
Hi, In my web form, I have a Text field with Calendar exterder added to allow the datetime entry, I used this code
StartDate =Convert.ToDateTime(StartDate.Text),
to save the data to SQL database, but it saved as something like: 2012-10-24 00:00:00 000,
Now, I need to convert it to date only, so I changed the code as this:
StartDate = Convert.ToDateTime(StartDate.Text).ToShortDateString but it does not work,
Does anybody know how to change the code to achieve my goal, THanks a lot,
Ramu Ajay
Member
623 Points
203 Posts
Re: About using Convert.ToDateTime(StartDate.Text).ToShortDateString, help please,
Jan 02, 2013 06:53 PM|LINK
Try
StartDate = Convert.ToDateTime(StartDate.Text).ToString("d");
What is the Column Datatype you are tryinng to save in?
budugu
All-Star
41102 Points
6016 Posts
Re: About using Convert.ToDateTime(StartDate.Text).ToShortDateString, help please,
Jan 02, 2013 06:54 PM|LINK
If you are using SQL2008, change your column datatype to DATE.
"Don't be afraid to be wrong; otherwise you'll never be right."
sen338
Member
498 Points
118 Posts
Re: About using Convert.ToDateTime(StartDate.Text).ToShortDateString, help please,
Jan 02, 2013 06:55 PM|LINK
Check you database DataType on that column whether it is a DateTime if so change it to Date .
geniusvishal
Star
13984 Points
2783 Posts
Re: About using Convert.ToDateTime(StartDate.Text).ToShortDateString, help please,
Jan 02, 2013 07:05 PM|LINK
Modify the stored procedure.. use correct date format..
Refer:
http://stackoverflow.com/questions/1320694/storing-the-date-only-in-sql-server-2005
http://www.sql-server-helper.com/tips/date-formats.aspx
My Website
www.dotnetvishal.com
Peter Cong
Member
527 Points
681 Posts
Re: About using Convert.ToDateTime(StartDate.Text).ToShortDateString, help please,
Jan 02, 2013 07:10 PM|LINK
I tried to change the Datetype to Date, but I can not find it from the dropdown list of datatype, I tried to manually change it to Date, it shows "Invalid date type".
Is anyway to use the C# code to convert if I can not change it this way? Thanks
sen338
Member
498 Points
118 Posts
Re: About using Convert.ToDateTime(StartDate.Text).ToShortDateString, help please,
Jan 02, 2013 07:18 PM|LINK
Then You Should change DataType to Varchar(10) or while saving save as it is and while fetching record from DataBase convert date like this
select Convert(Varchar(10),DateCoulmn,101) as Column from Table
101 is a format
Pelase refer below site
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Peter Cong
Member
527 Points
681 Posts
Re: About using Convert.ToDateTime(StartDate.Text).ToShortDateString, help please,
Jan 02, 2013 07:35 PM|LINK
"Converts an expression of one data type to another in SQL Server 2012."
But, it seems this type of convertion is used on SQL Sever 2012, I am using SQL 2008 now. It may not work.
Peter Cong
Member
527 Points
681 Posts
Re: About using Convert.ToDateTime(StartDate.Text).ToShortDateString, help please,
Jan 02, 2013 07:37 PM|LINK
But I do not use stored procedure, I save data directly to SQL tables.
Peter Cong
Member
527 Points
681 Posts
Re: About using Convert.ToDateTime(StartDate.Text).ToShortDateString, help please,
Jan 02, 2013 07:43 PM|LINK
This code does not compile, I got blue line under the code "Can not implicitly convert type 'string' to 'System.Date.Time'
My column datatype of SQL table column is "DateTime"