Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

sql

asp.net

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

like image 237
emilios Avatar asked Mar 11 '26 17:03

emilios


2 Answers

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();
}
like image 139
Jeff Ogata Avatar answered Mar 13 '26 06:03

Jeff Ogata


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

like image 31
bhaveshkac Avatar answered Mar 13 '26 07:03

bhaveshkac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!