Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 1:53am
Comments
vinay raghavendr at 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
good
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!
  Permalink  
Comments
SChristmas at 30-Jan-13 7:43am
   
Nicely described Ankur :)
Ankur\m/ at 30-Jan-13 7:49am
   
Thanks!
Pete O'Hanlon at 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/ at 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 at 31-Jan-13 3:53am
   
5+
Rahul Rajat Singh at 31-Jan-13 4:08am
   
The best answer. +5.
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

You should take Datetime datatype in your database.

Thanks
  Permalink  
Comments
Rahul Rajat Singh at 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
good
Please Sign up or sign in to vote.

Solution 7

use DateTime data type
  Permalink  
Comments
Rahul Rajat Singh at 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/ at 31-Jan-13 4:27am
   
I guess the downvote was for repeating the same answer. The answerer added nothing new.
AshishChaudha at 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.

Thanks
 

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

Solution 5

hi
 
check it
 
DateTime myDate ;
myDate= Convert.ToDateTime(Txtox1.Txt);
 

  Permalink  
Rate this: bad
good
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//
 
_date
2013-01-01 00.00.00
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
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