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

  • SEARCH
  • Home
  • 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 305685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:23:49+00:00 2026-05-12T07:23:49+00:00

A common problem in LINQ2SQL is while the .NET String allows assigning any length

  • 0

A common problem in LINQ2SQL is while the .NET String allows assigning any length to its variable, your database may have a specific max length constraint (like VARCHAR(5)). This will lead to the SQL error message “String or binary data would be truncated.”, which is extremely unhelpful since it doesn’t tell you which fields is the culprit.

Obviously, validating the input for the maximum string length will be the correct way. The main problem I faced is creating the necessary validation for every LINQ object in my project, and updating the validation if the maximum length of the field is updated.

Ideally, I need to work out a way to dynamically determine the max length of a generated field, so I do not risk forgetting to update the validation later.

The best possible implementation I can find so far is “Integrating xVal Validation with Linq-to-Sql”, which is already far superior to anything I can think. The only uncertain point is the dynamically determine the max length.

Has anyone seen or implemented anything similar?

  • 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-12T07:23:49+00:00Added an answer on May 12, 2026 at 7:23 am

    The LINQ2SQL code generator places an attribute on property fields similar to:

    [Column(Storage="_Message", DbType="NVarChar(20)")]
    

    It would be simple to extract and use this information at runtime:

    public class Row
    {
        // normally generated by LINQ2SQL
        [Column(Storage = "_Message", DbType = "NVarChar(20)")]
        public string Message
        {
            get;
            set;
        }
    
        // normally generated by LINQ2SQL
        [Column(Storage = "_Property", DbType = "NVarChar(20) NOT NULL")]
        public string Property
        {
            get;
            set;
        }
    }
    
    public class VarCharInfo
    {
        public int? MaxLen;
        public bool IsNullable;
    }
    
    public static VarCharInfo GetVarCharInfo(PropertyInfo propertyInfo)
    {
        var attrib = propertyInfo.GetCustomAttributes(typeof(ColumnAttribute), false)
            .OfType<ColumnAttribute>()
            .FirstOrDefault();
    
        if (attrib == null || attrib.DbType == null)
        {
            return null;
        }
    
        var match = Regex.Match(attrib.DbType, @"VarChar\((?'len'\d+)\)(?'notnull' NOT NULL)?");
    
        if (!match.Success)
        {
            return null;
        }
    
        var rvl = new VarCharInfo();
    
        rvl.MaxLen = int.Parse(match.Groups["len"].Captures[0].Value);
        rvl.IsNullable = match.Groups["notnull"].Success;
    
        return rvl;
    }
    
    public static bool ValidateVarCharColumns(object dataObject)
    {
        bool success = true;
    
        foreach (var propertyInfo in dataObject.GetType()
            .GetProperties()
            .Where(pi => pi.PropertyType == typeof(string)))
        {
            var vci = GetVarCharInfo(propertyInfo);
    
            if (vci != null)
            {
                var currentValue = propertyInfo.GetGetMethod()
                    .Invoke(dataObject, null) as string;
    
                if (currentValue == null)
                {
                    if (!vci.IsNullable)
                    {
                        // more work: value is null but it shouldn't be
                        success = false;
                    }
                }
                else if (vci.MaxLen != null && currentValue.Length > vci.MaxLen)
                {
                    // more work: value is longer than db allows
                    success = false;
                }
            }
        }
    
        return success;
    }
    
    static void UsageTest()
    {
        Row t = new Row();
        t.Message = "this message is longer than 20 characters";
        // t.Property is null
    
        ValidateVarCharColumns(t);  // returns false!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

A common problem in any language is to assert that parameters sent in to
I guess it's quite a common problem in databinding scenarios. What do you usually
The scenario is this We have two applications A and B, both which are
I have command button added in my asp.net grids. After performing an action using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.