Wednesday, March 7, 2012

DateTime & SQL Server

I'm going slowly insane trying to get a Date AND Time value stored in SQL 2000 from ASP.NET 2.0.

The following works fine in Query Analyzer, but fails when run from ASP.NET:

INSERT INTO tbl_WIP_DATA_DateTime (ObjectInstanceID, ObjectPropertyID, PropertyData_DateTime) VALUES (32226,7,'20060120 10:43:44')

Does anyone have any suggestions?

Cheers
Vatic

Is that exactly what you have in your ASP.NET page (hardcoded values) or are you passing the values through parameters? If you are using parameters please post code.|||

This is exactly as it appears in code. The sql query is dynamically created.

|||

I've also tried the following:

Dim objconAsNew SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Item("ConnectionString"))
Dim myCommandAs SqlCommand
Dim myParamAs SqlParameter
myCommand =New SqlCommand()
myCommand.Connection = objcon

DataQuery ="INSERT INTO " & myProperty.dataTable &" (ObjectInstanceID, ObjectPropertyID, " & myProperty.DataColumn &") VALUES (@.ObjectInstanceID, @.ObjectPropertyID, @.theDateTime)"

myCommand.CommandText = DataQuery
myCommand.Parameters.Add(New SqlParameter("@.ObjectInstanceID", SqlDbType.Int))
myCommand.Parameters("@.ObjectInstanceID").Value = myObjectInstance.ObjectInstanceID

myCommand.Parameters.Add(New SqlParameter("@.ObjectPropertyID", SqlDbType.Int))
myCommand.Parameters("@.ObjectPropertyID").Value = myProperty.ObjectPropertyID

myCommand.Parameters.Add(New SqlParameter("@.theDateTime", SqlDbType.DateTime))
myCommand.Parameters("@.theDateTime").Value = Now()

objcon.Open()
myCommand.ExecuteNonQuery()
objcon.Close()

Which returns:

The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.Expected type is DateTime

|||

Try:

dim dt as datetime=datetime.parse(Now())

See if that throws you any errors.

No comments:

Post a Comment