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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Create_Date" format to save records

Status
Not open for further replies.

pinkpoppy

Programmer
Jul 6, 2011
75
0
0
US
I have this piece of code that is going to save records in my application. I found that the "create_date" has to be format in this way:
"Create_Date": string; required; must validate as a 'DateTime value'. Suggest format: '2012-10-08 02:49:15 AM'


How do you make r1["Create_Date"] = "getdate()"; in the suggested format?


Code:
  if (Form1.Rows.Count > 0)
                {
                    DataRow r1 = dt1.NewRow();                   
                    r1["Clerk_ID"] = Convert.ToInt32(dtDataForm1.Rows[0]["Clerk_ID"]);
                    r1["County"] = dtDataForm1.Rows[0]["County"].ToString();
                    r1["Month"] = Convert.ToInt32(dtDataForm1.Rows[0]["month"]);
                    r1["Year"] = Convert.ToInt32(dtDataForm1.Rows[0]["year"]);
                    r1["Create_User"] = "suser_sname()";
                    r1["Create_Date"] = "getdate()";
                    r1["Modify_User"] = "";
                    r1["Modify_Date"] = "";
                    dt1.Rows.Add(r1);
                }


Suggestions are very much appreciated!
 

You're mixing T-SQL server functions with DataRow record population. Getdate() in SQL will return a valid datetime based on the current time and date of the server. If you're populating CREATE_DATE with the current date and time you'll need to use Now() in your code rather than getdate(). You'll also probably find that the CREATE_USER column populated with the literal string "suser_sname" rather than the SQL user name I think you're expecting.

To use getdate() and suser_sname you'll have to create a stored procdure or insert statement to be exectured on the SQL Server, not as column values.




Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Thanks for the reply.

How do I write that?
 
Would it be possible to just set the default values of the database table's columns to getdate() and suser_sname()?

Then, you just let nulls get inserted and the db will handle the rest.


good luck!

-Mark
 
Thanks for the reply.
I've already find the solution!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top