This is a quick guide for how to fix the issue 'String or binary data would be truncated' error when using Umbraco Forms.

We came across the issue after an upgrade of Umbraco Forms. The problem is this: Umbraco Forms on form submission tries to save the data into the database, however the data exceeds the allowed storage in the database. There are two issues that can cause this to occur, these are discussed below.

[Issue 1] TextField maxlength

The Umbraco Issue tracker describes this is in detail, basically the textfield maxlength is longer than the maxlength of the field that stores it in the database. The issue is discussed on the Umbraco issue tracker here.

There are two possible fixes for this first issue, either:

  • update the frontend FieldType view, or
  • update the table definition in the backend database

[Fix 1] Update the definition for the Field Type in your FieldType.TextField.cshtml view

You can set the maxlength property of the attribute by navigating to the view here. Navigate to your FieldType view, this is usually found at: Views > Partials > Forms > <your_theme> > fieldTypes > FieldType.TextField.cshtml

<input type="text" 
       name="@Model.Name" 
       id="@Model.Id" 
       class="@Html.GetFormFieldClass(Model.FieldTypeName) text" 
       value="@Model.ValueAsHtmlString" 
       maxlength="255" />

Set the maxlength value to 255 rather than 500 so it matches the field length in the database. This should be okay for most circumstances. However, if you require it be longer than 500 characters then try the [fix 2].

[Fix 2] Increase the length of the 'value' field in the UFRecordDataString table to navarchar(500)

The alternative is go into the database via SQL Server Management Studio, then navigate to the UFRecordDataString table. Update the table definition, as seen below, change the datatype to nvarchar(500) to match the allowed maxlength.

UFRecordDataString-data-type-value

[Issue 2] AliasField maxlength

The other cause of this error is when the form is created the aliases for the fields are automatically generated based on the caption. The length of the caption can exceed 50 or 255 characters. However, the database definition for that field is set to nvarchar(50) or nvarchar(255) which, if exceeded throws this error. There is more detail about this issue on the Umbraco issue tracker.

The fix is similar to the above solution, go into management studio and change the alias definition nvarchar to nvarchar(max) in the table UFRecordFields.

update-alias-ufrecordfields

Is this best practice?

There is some argument that changing the database definitions could cause issues down-the-line when upgrading Umbraco or Umbraco Forms. So I'd advise treating these fixes as a work around until new versions of Umbraco Forms fix these issues.