Click to See Complete Forum and Search --> : Date / Time Help


pre_wreck
October 26th, 2006, 01:40 AM
Hi Guru's,

I have a problem, please I need your help.

I have here a piece of code, that returns a syntax error.


string sSQL;
sSQL = "INSERT INTO tblLoginDetails(Userid, Activity, DateTime)";
sSQL += "VALUES";
sSQL += "('" + sUserID + "', '" + sActivities + "', '" + System.DateTime.Now + "')";



Table properties:

Userid = text or string
Activity = text or string
DateTime = Date/Time = General Date Format


Thanks in advance and more power.

naveenl
October 26th, 2006, 03:05 AM
Posting error information will be useful.....

Is sUserID, sActivities strings initialized????

HanneSThEGreaT
October 26th, 2006, 03:45 AM
At a guess....
I'd say that you are using ' for the DateTimeField instead of #
sSQL += "('" + sUserID + "', '" + sActivities + "', '" + System.DateTime.Now + "')";



Try this :
sSQL += "('" + sUserID + "', '" + sActivities + "', #" + System.DateTime.Now + "#)";

I hope it helps! :)

darwen
October 26th, 2006, 04:19 AM
You should be using an SqlCommand to do this. This has a Parameters collection property which takes care of the parameters syntax for you :

e.g.


SqlConnection connection = // from wherever

string query = "INSERT INTO tblLoginDetails(Userid, Activity, DateTime) VALUES (@USERID, @ACTIVITY, @DATETIME)";

string userId = "1234";
string activity = "hello";
DateTime dateTime = DateTime.Now;

using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.Add("@USERID", userId);
command.Parameters.Add("@ACTIVITY", activity);
command.Parameters.Add("@DATETIME", dateTime);

command.ExecuteNonQuery();
}


If you're using OLE or Oracle connections then you need to use the appropriate command class for these.

Darwen.