Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 How to get the system date and time into database

Author  Topic 

krishnet
Starting Member

29 Posts

Posted - 2007-06-03 : 01:14:21
Hi,

I m using ASP.NET with C#.
I m having one field which should store the datetime of the system.

The datetime should be automatically stored for that entry when the user submits that record on the click event.
Then it should display the date time on the gridview from database.

I m using sqlserver 2005 and i have created the stored procedure for the insert command.

This is the sample sp what should be written here to insert system date time automatically when the user submits the asp.net form ?

ALTER PROCEDURE [dbo].[StoredProcedure1]

@salesid INT OUTPUT,
@salesdate datetime,
@customername varchar(20)

AS
BEGIN
SET NOCOUNT ON

BEGIN
INSERT INTO sales (customername)
VALUES (@customername)
SELECT @companyid = SCOPE_IDENTITY()
END

SET NOCOUNT OFF
END



Thanxs in advance...

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-06-03 : 01:25:59
best would be to use a default value of getdate() or getutcdate() for your datetime column:


create table mytable
(
id int primary key not null,
datecol datetime default getdate(), -- note the default value here
customername varchar(100) not null
)



www.elsasoft.org
Go to Top of Page

krishnet
Starting Member

29 Posts

Posted - 2007-06-03 : 01:41:02
Thanxs for ur reply first of all..

Will the getdate() function fetches the current system date and time both together into the database ??
How to note the default value it changes everyday...

The user is not at all allowed to enter the date and the time..

Can u pls tell me more clearly..Ur way is the easiest but sorry i m not able to get it...
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2007-06-03 : 09:40:28
the getdate() function grabs the current date and time.

if you default the column to getdate() as jezemine suggests, then you do not have to explicitly insert any data into that column. it will automatically default to the current getdate value when a row is inserted.

if you want more information on getdate(), you should read up about it in BOL. here is the link http://msdn2.microsoft.com/en-us/library/ms188383.aspx



-ec
Go to Top of Page

krishnet
Starting Member

29 Posts

Posted - 2007-06-03 : 10:29:42
thanxs very much for both of ur replies...
Go to Top of Page
   

- Advertisement -