3
votes

if you please help me out my error is:

Conversion failed when converting date and/or time from character string.

my database column is of type datetime

2
What value is in the ctime variable at this point? That should give a clue as to why it throws the error.Dan J

2 Answers

9
votes

Use a parameterized query and you won't have to worry about date formats, or sql injection, and use using to ensure your connection is disposed.

using (var connection = new SqlConnection(yourConnectionString))
using (var command = connection.CreateCommand())
{
   command.CommandText = "insert into YourTable(Col1, Col2) values(@val1, @val2)";
   command.Parameters.AddWithValue("@val1", 123);
   command.Parameters.AddWithValue("@val2", DateTime.Now);

   connection.Open();

   command.ExecuteNonQuery();
}
2
votes

you can also GETDATE() function of sql it is predefine function of sql