Fiaz Ahmed Ranjha
Fiaz Ahmed Ranjha

Reputation: 253

string or binary data would be truncated. when large string is inserted in a field

in my model in asp.net mvc5 website there is field name

   [Display(Name = "Detail (A-Z)")]
            [StringLength(int.MaxValue)]
            [AllowHtml]
            public string Description { get; set; }

i want to have maximum string size for this field . i used StringLength(int.Maxvalue) when i enter large string(not very large) it gives error .on small string it works fine. i changed the removed the stringlength data annotation but still gives the same error.i think stringlangth should allow maximum characters.

Upvotes: 0

Views: 139

Answers (1)

Md Rahatur Rahman
Md Rahatur Rahman

Reputation: 3244

It is not the problem with the Server side code. The issue is with the database's field's length. You are sending longer string than the DB column can hold. Thus the error is raised for longer strings.

Do the followings: [StringLength(X)] here x is the length of the column in the DB.

If you want the input to be max in length which is required in case of html inputs, then remove the StringLength property from the model. Which would not restrict the input length. And then change the DB column to be Varchar (Max).

Upvotes: 1

Related Questions