Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting the current date into a table when insert 1

Status
Not open for further replies.

thefrstgatsby

Technical User
Joined
May 8, 2004
Messages
113
Location
CA
How do I insert the current date and time into my sql datetime row, whenever someone submits a form?
 
1. In the table definition, set the Default Value to :
Code:
(getdate())
When a record will be added to the table, that column will be set to the current date time set on the SQL server.
2. From your app code:
Code:
string strNow = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
Pass strNow to INSERT or UPDATE command that you build and send to the server.
3. Pass it as parameter from your app code to a stored procedure:
Code:
string strNow = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
//...

SqlDataAdapter vSqlDataAdapter=new SqlDataAdapter();
vSqlDataAdapter.SelectCommand = new SqlCommand("sp_MyStoredProc");
vSqlDataAdapter.SelectCommand.CommandType =CommandType.StoredProcedure;
//... 
vSqlParameter= vSqlDataAdapter.SelectCommand.Parameters.Add("@ColumnToBeSetToNow", strNow);

The sp_MyStoredProc is a stored procedure which receives the value of strNow and does the job.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top