"String or binary data would be truncated." linq exception, cant find which field has exceeded max length

Viewed 9470

String or binary data would be truncated. linq exception, cant find which field has exceeded max length.

i have around 350 fields. i checked each and every textbox maxlength with database field maxlength, everything seems to be correct, but i still get the exception.

please help

4 Answers

And, hey, why not have the same solution (@shaunmartin's) in VB.Net, too? Sometimes you just gotta debug someone else's code!

Use:

            For Each update as Object In context.GetChangeSet().Updates
                FindLongStrings(update)
            Next

            For Each insert as Object In context.GetChangeSet().Inserts
                FindLongStrings(insert)
            Next

And core

   Public Shared Sub FindLongStrings(ByVal testObject As Object)
        Dim propInfo As PropertyInfo
        For Each propInfo In testObject.GetType().GetProperties()
            Dim attribute As ColumnAttribute
            For Each attribute In propInfo.GetCustomAttributes(GetType(ColumnAttribute), True)
                If attribute.DbType.ToLower().Contains("varchar") Then
                    Dim dbType As String = attribute.DbType.ToLower()
                    Dim numberStartIndex As Integer = dbType.IndexOf("varchar(") + 8
                    Dim numberEndIndex As Integer = dbType.IndexOf(")", numberStartIndex)
                    Dim lengthString As String = dbType.Substring(numberStartIndex, (numberEndIndex - numberStartIndex))
                    Dim maxLength As Integer = 0
                    Integer.TryParse(lengthString, maxLength)

                    Dim currentValue As String = CType(propInfo.GetValue(testObject, Nothing), String)

                    If Not String.IsNullOrEmpty(currentValue) AndAlso maxLength <> 0 AndAlso currentValue.Length > maxLength Then
                        Console.WriteLine(testObject.GetType().Name & "." & propInfo.Name & " " & currentValue & " Max: " & maxLength)
                    End If

                End If
            Next
        Next
    End Sub

But with a lot of data or a lot of fields this takes a lot of time for sure. The debugging side of linq is lacking - the exception thrown should tell you the field!

i set the max length for all the 350 fields. i guess thats the only way. thanks for your support.

Related