views:

26

answers:

2

I have a number of pending changes in my object context when I call SaveChanges. Somewhere in there is an entity with a value for a column that is too long. This results in SqlException: String or binary data would be truncated.

The question is how do I determine offending entity/column?

A: 

SQL Profiler.

Dave Swersky
your funny -- Seriously I need to be able to tell the user, fix field x because it's too long. I may be wrong but flashing up a message like "something went wrong, because of your data, please launch SQL Profiler and figure it out for yourself" may not fly with the users.
Ralph Shillington
Though short, Dave's answer isn't "wrong"... you didn't specify that you needed to display an error to a user... you DID specify that you needed to find out what went wrong. SQL Profiler will tell you that information. DataAnnotations with Buddy Classes will help you perform validation for your users. Check out my answer below.
rockinthesixstring
Indeed, I misunderstood that this was a validation issue. Validation should be done on the front end to prevent this error. Each property that has a length limit should be annotated to allow for notification when a value is invalid.
Dave Swersky
@Dave, that's exactly right. See my answer RE: `DataAnnotations`
rockinthesixstring
+1  A: 

You could consider using DataAnnotations and building your Buddy Classes for validation. Then you display a friendly validation errors to your user if their data is incorrect.

Imports System.ComponentModel.DataAnnotations 

Namespace Domain 
#Region "Validation" 

<MetadataType(GetType(UserMetaData))> _ 
Partial Public Class User 
End Class 


''' <summary> 
''' Validation for all User data. 
''' </summary> 
''' <remarks>All validation is done at the Service Layer</remarks> 
Public Class UserMetaData 

    <DisplayName("name")> _ 
    <Required(ErrorMessage:="Username is required.")> _ 
    <StringLength(30, ErrorMessage:="Username cannot exceed 30 characters.")> _ 
    <RegularExpression("^\w{3,30}$", ErrorMessage:="Not a valid username.")> _ 
    Public Property UserName As String 

    <DisplayName("email")> _ 
    <StringLength(50, ErrorMessage:="Email Address cannot exceed 50 characters.")> _ 
    <RegularExpression("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$", ErrorMessage:="Not a valid email address.")> _ 
    Public Property Email As String 

    <DisplayName("website")> _ 
    <StringLength(256, ErrorMessage:="Web Address cannot exceed 256 characters.")> _ 
    <RegularExpression("^http(s?)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$", ErrorMessage:="Not a valid website address.")> _ 
    Public Property WebSite As String 

    <DisplayName("about")> _ 
    <StringLength(2000, ErrorMessage:="Profile cannot exceed 2000 characters.")> _ 
    Public Property About As String 

    <DisplayName("region")> _ 
    <Required(ErrorMessage:="Region is required.")> _ 
    Public Property UserRegion As Integer 

    <DisplayName("birthdate")> _ 
    <DisplayFormat(ApplyFormatInEditMode:=True, ConvertEmptyStringToNull:=True, DataFormatString:="{0:MM/dd/yyyy}")> _ 
    Public Property BirthDate As DateTime 

End Class 
#End Region 
End Namespace

More references

http://adventuresdotnet.blogspot.com/2009/08/aspnet-webforms-validation-with-data.html
http://blogs.msdn.com/b/jimoneil/archive/2008/07/08/dynamic-data-annotations.aspx
http://www.ipreferjim.com/site/2010/05/system-componentmodel-dataannotations-for-asp-net-web-forms/

rockinthesixstring