I have this problem recently. I saved the βstatusβ message in the nvarcharMAX database field, which is 4000 characters. However, my status messages accumulated and fell into the exception.
But this was not a simple case of truncation, since arbitrary truncation could lead to the loss of part of the status message, so I really needed to "truncate" the agreed part of the line.
I solved the problem by converting the string to a string array, deleting the first element, and then restoring the string. Here is the code ("CurrentStatus" is a string containing data) ...
if (CurrentStatus.Length >= 3750) { // perform some truncation to free up some space. // Lets get the status messages into an array for processing... // We use the period as the delimiter, then skip the first item and re-insert into an array. string[] statusArray = CurrentStatus.Split(new string[] { "." }, StringSplitOptions.None) .Skip(1).ToArray(); // Next we return the data to a string and replace any escaped returns with proper one. CurrentStatus = (string.Join(".", statusArray)) .Replace("\\r\\n", Environment.NewLine); }
Hope this helps someone.
Darren Street Jan 29 '18 at 16:20 2018-01-29 16:20
source share