Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am trying to get the current time/date and insert it in database table but i get error.

DateTime DateTimeVariable = DateTime.Now.Date;

                       SqlConnection con1 = new SqlConnection("Data Source=MZC-RWAHDAN\\MYPROJECT2015;Persist Security Info=True;User ID=sa; Password=Fatima2010;Initial Catalog=Testing_database");

                       SqlCommand command1 = new SqlCommand("insert into TrackIt (FullName,UserName,actiontype,actiondesc,actiontime) VALUES(@param1,@param2,@param3,@param4,@param5", con1);

                      command1.Parameters.Add(new SqlParameter("@param1", DataReader["FullName"]));
                      command1.Parameters.Add(new SqlParameter("@param2", DataReader["username"]));
                      command1.Parameters.Add(new SqlParameter("@param3", "Logged_IN"));
                      command1.Parameters.Add(new SqlParameter("@param4", "Logged in as Admin to Admin_Page"));
                      command1.Parameters.Add(new SqlParameter("@param5", DateTimeVariable));


I am getting the full name and the username data from earlier query but the rest (3,4) i am entering text and the last one (5) i need to enter data/time for tracking. i get the error:

Incorrect syntax near '@param5'.

Please help,

Garth J Lancaster 19-Dec-15 1:57am    
what is the definition for the TrackIt table - in particular the actiontime column ?
Garth J Lancaster 19-Dec-15 2:00am    
btw, Im not sure about this -> DateTime DateTimeVariable = DateTime.Now.Date; ... whats wrong with DateTime.Now; (unless the column is defined as a string of course)

If you have datetime datatype in your table than insert the datetime format only thats why you are getting error. Datetime.Now will solve your problem.

DateTime DateTimeVariable = DateTime.Now;
Share this answer
Member 11414035 11-Jan-16 23:49pm    
You have missed the closing bracket.

insert into TrackIt (FullName,UserName,actiontype,actiondesc,actiontime) VALUES(@param1,@param2,@param3,@param4,@param5

Correct SQL is :

insert into TrackIt (FullName,UserName,actiontype,actiondesc,actiontime) VALUES(@param1,@param2,@param3,@param4,@param5)

Fix is

SqlCommand command1 = new SqlCommand("insert into TrackIt (FullName,UserName,actiontype,actiondesc,actiontime) VALUES(@param1,@param2,@param3,@param4,@param5)", con1);
Share this answer
Member 3651989 19-Dec-15 2:18am    
i did that now i am getting a new error

String or binary data would be truncated.
The statement has been terminated.
_Asif_ 19-Dec-15 2:30am    
It means that one of your column have received data larger than their allocated space.
You need to extend your column sizes and try again. Check which column have got broken

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900