I'm trying to insert a date time value into a SQL Server Database field of type date time using a c# web form.
I get the following error...
"The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. The statement has been terminated"
My SQL looks like this:
INSERT INTO tblname (fldname) VALUES ('" + DateTime.Now.ToString() + "')"
What am I doing wrong?Remove the ToString() method as that is changing the DateTime type to a string (or Char type) ...at least thats what it looks like.
Nope sorry to say that didn't work.
My C# now looks like
SqlConnection myConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["eTeam_ConnectionString"].ToString());
string mySQL = "INSERT INTO tblname (datestamp) VALUES ('" + DateTime.Now + "')";
SqlCommand myCMD = new SqlCommand(mySQL, myConn);
myConn.Open();
myCMD.ExecuteNonQuery();
myConn.Close();
myCMD.Dispose();
myConn.Dispose();
And the field 'datestamp' is a SQL Server Data Type datetime
Anyone else?
You can use getDate() method of SQL server to update date.
INSERT INTO tblname (datestamp) VALUES (getDate())";
Thanks for that - definately half way to solving my problem.
What if I need to take a value from an ASP:Calendar control?
Is there a ToDate() function that will convert it as required?
You can use
Calendar1.SelectedDate.ToShortDateString())
which will convert this date into short date format.
0 comments:
Post a Comment