Conversion Of C# Datetime To Sql Server Datetime Is Throwing An Error
In C# a DateTime property with value {27-01-2017 12.00.00 AM} is being passed in a data table to a procedure with an UTT parameter. UTT also has the same datatype datetime. I am us
Solution 1:
Your code - as it is now - will transfer any value on string level. This is a really bad approach. The implicit conversions taking place are highly depending on your system's settings (language and culture). The worst part is: This might work all great on your machine while you are testing it, but on a customer's system it breaks with strange messages. Happy Debugging :-(
Change your code like this
foreach (PropertyInfo prop in props) {
// Setting column names as Property names.if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
dataTable.Columns.Add(prop.Name, prop.PropertyType.GetGenericArguments()[0]);
else
dataTable.Columns.Add(prop.Name, prop.PropertyType);
}
This will add the column - even if this is a nullable type - with the correct data type.
credits:This answer helped me
UPDATE Even simpler
(thx to Yves M. in a comment below the linked answer)
foreach (PropertyInfo prop in props) {
// Setting column names as Property names.
dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
Solution 2:
Remove the quotation marks
"@UttParameter"
@UttParameterSolution 3:
You are using InvariantCulture as DataTable locale. Invariant culture expects Date to be in yyyy-MM-dd format.
Post a Comment for "Conversion Of C# Datetime To Sql Server Datetime Is Throwing An Error"