pinki
pinki

Reputation: 1418

‘String or binary data would be truncated’

I am using entity framework to insert details into the database where I have a column varchar(50). when I am trying to insert some value more than 50 length it gives me an error string or binary data would be truncated.So ., for safe side I just changed it to varchar(100).

Now can someone let me know is this the only solution to change the value in the Db or do we have any alternatives....... I read an article http://sqlblogcasts.com/blogs/danny/archive/2008/01/12/scuffling-with-string-or-binary-data-would-be-truncated.aspx

But how can I use such things in c#. I appreciate any suggestions from you.........

Thanks

Upvotes: 2

Views: 5237

Answers (4)

Naveed ahsan
Naveed ahsan

Reputation: 1

1-when database table Alter when issue generated
2-First Backup table schema and data then delete table when re create table then run code .. working fine.

Upvotes: 0

SabbaTh
SabbaTh

Reputation: 136

Depending on what type of input field you're dealing with, using varchar(max) could be an option.

But as previously pointed out, it really boils down to what your business requirements are.

Upvotes: 2

Mikael Eriksson
Mikael Eriksson

Reputation: 139010

You should set the field length in the db to whatever is a reasonable length and you should prevent users from entering values that does not fit within that length.

Upvotes: 0

Pleun
Pleun

Reputation: 8920

Well, first of all, obviously you cannot insert a string longer than 50 in a varchar(50).

So you have two options depending on your requirement:

  • change the database (as you have found out) and make sure that all code 'upstream' will be able to tackle the longer data
  • add some validations or restrict user input so that you will never get a string that is longer

Well, there's a third and that is cut off the string without telling the user but I would not do that.

So it depends on your business requirement what to do. But I would not do any 'tricks' like in the article you suggested

Upvotes: 2

Related Questions