Hi friends,
I am assigning API datetime value to a string like the below in our .net core project.
string strdate = Convert.ToString(token["Receipt Date"]) + " " + Convert.ToString(token["Transaction Time"]);
I am inserting into the table like the below.this worked these many days without fail.
cmd.Parameters.AddWithValue("@Saledatetime",Convert.ToDateTime(strdate));
But now when I am using Convert.ToDateTime(strdate) it is giving the value {2023-11-27 12:41:04}.
So getting the error while insertion.In the string it is coming value 2023-11-27 12:41:04 Why the curly braces { } coming fter converting string to datetime.
Please tell me how to resolve this?

Amit MohantyRank #112 · 20K reputationPosted Nov 28, 2023, 2:59 AM EST
Try this:
string receiptDate = Convert.ToString(token["Receipt Date"]); string transactionTime = Convert.ToString(token["Transaction Time"]); string strdate = $"{receiptDate} {transactionTime}"; DateTime parsedDateTime; if (DateTime.TryParseExact(strdate, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDateTime)) { cmd.Parameters.AddWithValue("@Saledatetime", parsedDateTime); } else { // Handle the case where the string couldn't be parsed as DateTime. }prasanna pRank #3,234 · 443 reputationPosted Nov 28, 2023, 3:08 AM EST
hi Amit Mohanty,
Getting the same error.
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
In the parsedDateTime field have value {2023-11-27 12:41:04}
Amit MohantyRank #112 · 20K reputationPosted Nov 28, 2023, 3:59 AM EST
Check the demo here: https://www.mycompiler.io/view/JJqMRJUpI4d