Joe
Joe

Reputation: 81

String or binary data would be truncated. The statement has been terminated

I have an insert statement before these updates: On db.sumbitchanges the error is shown.

Topic top = (from t in db.Topics
                 where t.id == id
                 select t).SingleOrDefault();

top.lastpost = username + maxdate;


Category ca = (from c in db.Categories
             where c.categoryid == cat
             select c).SingleOrDefault();

ca.totaltopics = ca.totaltopics + 1;
ca.posts = ca.posts + 1;
ca.lastpost = username + maxdate;
db.SubmitChanges();     

Upvotes: 0

Views: 7571

Answers (3)

Anton Gogolev
Anton Gogolev

Reputation: 115731

I assume both Topic.lastpost and Category.lastpost are strings, and username + maxdate concatenates two strings. The result might be a bit bigger than what would fit into lastpost column in respective table.

Upvotes: 1

Dave D
Dave D

Reputation: 8972

It sounds like either top.lastpost or ca.lastpost (or both) doesn't have enough space on the db to hold username + maxdate.

Check how many characters the database fields allow and either change the field to allow more characters or reduce the length of the output - perhaps only storing username + maxdate.ToString("yyyy-MM-dd") or username + maxdate.ToString("yyyy-MM-dd HH:mm:ss")?

Upvotes: 4

rslite
rslite

Reputation: 84683

The string is longer than the size of the DB column so the resulting data doesn't fit inside.

Upvotes: 0

Related Questions