Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3215552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:11:28+00:00 2026-05-17T15:11:28+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:11:28+00:00Added an answer on May 17, 2026 at 3:11 pm

    Troubleshooting this error with 350 fields can be extremely difficult, and SQL Server Profiler isn’t much help in this case (finding the long string in the generated SQL is like finding a needle in a haystack).

    So, here is an automated way to find the actual strings that are exceeding the database size limit. This is a solution that’s out there on the internet, in various forms. You probably don’t want to leave it in your production code, since the attribute/property searching is pretty inefficient, and it’ll add extra overhead on every save. I’d just throw it in your code when you encounter this problem, and remove it when you’re done.

    How it works: it iterates over all properties on an object you’re about to save, finding the properties with a LINQ to SQL ColumnAttribute. Then, if the ColumnAttribute.DbType contains “varchar”, you know it’s a string and you can parse that part of the attribute to find the maximum length.

    Here’s how to use it:

    foreach (object update in context.GetChangeSet().Updates)
    {
        FindLongStrings(update);
    }
    
    foreach (object insert in context.GetChangeSet().Inserts)
    {
        FindLongStrings(insert);
    }
    
    context.SubmitChanges();
    

    And here’s the method:

    public static void FindLongStrings(object testObject)
    {
        foreach (PropertyInfo propInfo in testObject.GetType().GetProperties())
        {
            foreach (ColumnAttribute attribute in propInfo.GetCustomAttributes(typeof(ColumnAttribute), true))
            {
                if (attribute.DbType.ToLower().Contains("varchar"))
                {
                    string dbType = attribute.DbType.ToLower();
                    int numberStartIndex = dbType.IndexOf("varchar(") + 8;
                    int numberEndIndex = dbType.IndexOf(")", numberStartIndex);
                    string lengthString = dbType.Substring(numberStartIndex, (numberEndIndex - numberStartIndex));
                    int maxLength = 0;
                    int.TryParse(lengthString, out maxLength);
    
                    string currentValue = (string)propInfo.GetValue(testObject, null);
    
                    if (!string.IsNullOrEmpty(currentValue) && maxLength != 0 && currentValue.Length > maxLength)
                        Console.WriteLine(testObject.GetType().Name + "." + propInfo.Name + " " + currentValue + " Max: " + maxLength);
    
                }
            }
        }
    }
    

    Update 12/03/2019 –
    This answer is referenced on Linqpad.net website for the same error. In Linqpad (version 5) (that uses LinqToSql) the columns are no longer listed as properties instead they are fields . Use the following to iterate through fields:

    foreach (FieldInfo propInfo in testObject.GetType().GetFields())
    ...
    ...
             string currentValue = (string)propInfo.GetValue(testObject);
    ...
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the following error from ExecuteNonQuery() String or binary data would be truncated.
I'm getting String or binary data would be truncated error when, I'm trying to
I have a string which contains binary data ( non-text data ). How do
I'm currently having trouble creating an image from a binary string of data in
Related Question: vector <unsigned char> vs string for binary data . My code uses
I have a String with binary data in it (1110100) I want to get
What is the fastest method for converting a binary data string to a numeric
I have question about interpreting strings as packed binary data in C++. In python,
I have a problem converting a string binary to a decimal I was using
Let a binary string composed of messages separated by one null byte: <message><null><message><null> ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.