Click here to Skip to main content
12,947,948 members (88,878 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i have to store only time in database from textbox.In textbox i insert only time .
what datatype i have to take for this?
ex:i enter start time in textbox and this value is store in database.if anything else ohetthen time value is try enter then msg near textbox display like ...invalid time.
Posted 30-Jan-13 0:53am
vinay raghavendr 30-Jan-13 7:04am
1.Write the validation for that TextBox which checks for dd-mm-yyyy format.
2.Since TextBox.Text value is always string convert to DateTime and assign to DateTime Variable
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You haven't mentioned which database server you are using. If it's SQL Server 2008 or above, you got time[^] as a new datatype. (Note: I am just considered SQL Server here. Oracle and other db may also support this out of the box).

If you using old versions of SQL, you can either store them in DateTime[^] or varchar to store the time. But you will need to extract the time part (either on db side or your .net code) while using it.

Hope it helps!
SChristmas 30-Jan-13 7:43am
Nicely described Ankur :)
Ankur\m/ 30-Jan-13 7:49am
Pete O'Hanlon 30-Jan-13 8:09am
And this is the correct answer (sort of). There is an alternative approach that allows you to store it as an int, which is the number of milliseconds since midnight, but that could be more trouble than it's worth.

Anyhoo, a 5 from me.
Ankur\m/ 30-Jan-13 8:36am
I never thought of that option. But yes, there will be lot of Maths involved to actually use the data. IMO, varchar seems to very well fit in his scenario as well. Get the value, store it. Just take care the value is valid time. Similarly getting the data back from db and showing it easy too.
And thanks for the vote! :)
Edo Tzumer 31-Jan-13 3:53am
Rahul Rajat Singh 31-Jan-13 4:08am
The best answer. +5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

When dealing with Time only fields in a database, I usually store them as decimal in the database and use the TimeSpan object to handle them in my applications using the TimeSpan.TotalMilliseconds property.
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

You should take Datetime datatype in your database.

Rahul Rajat Singh 31-Jan-13 4:07am
If the time datatype is available for you then it is the best choice. But if not then I think datetime should be the way to go. I don't know why the datetime answers are being down voted because it is more type safe and it has merits over storing the data as string.

The second way could be to give the interpretation responsibility to the application and store the time as decimal in the DB(as suggested by Marcus already) but this way the OLAP guys will not work on the DB directly and they will have to rely on the conversion of this value to the time value everytime they need to use it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 7

use DateTime data type
Rahul Rajat Singh 31-Jan-13 4:09am
same argument here as for the above answer. I support it. Infact the highest voted answer also say the same thing but with some better suggestion.
Ankur\m/ 31-Jan-13 4:27am
I guess the downvote was for repeating the same answer. The answerer added nothing new.
AshishChaudha 31-Jan-13 4:57am
I think so..but this solution I had reposted and deleted the first one.The same solution I had given first. I am pasting it as I can view the deleted one

You should take Datetime datatype in your database.


Improve solution Permalink

Posted 21 hrs ago
Ankur\m/ 31-Jan-13 5:01am
Did you delete it? I can still see it there.
AshishChaudha 31-Jan-13 5:34am
I reposted the same.
Rate this: bad
Please Sign up or sign in to vote.

Solution 5


check it

DateTime myDate ;
myDate= Convert.ToDateTime(Txtox1.Txt);
Rate this: bad
Please Sign up or sign in to vote.

Solution 9

create table demo
_date datetime -- declare it as datetime if using sql 2005 in 2008 there is new datatype time

// in aspx.cs file
sqlcommand cmd= new sqlcommand("
insert into demo values( convert(nvarchar(10),'"+TextBox1.Text+"',101))",cn);

out put//

2013-01-01 00.00.00

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,344
CHill60 3,275
Maciej Los 2,628
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100