how to get size of a database column

Thread is closed for posting
2 posts, 1 answers
  1. Remco
    Remco avatar
    4 posts
    Member since:
    Oct 2013

    Posted 14 Mar 2014 Link to this post


    i get the following error:

    DataStoreException: Update failed: Telerik.OpenAccess.RT.sql.SQLException: String or binary data would be truncated.

    My database look like this:

    varchar(31) Name;
    varchar(31) FirstName;

    and the generated Enty Model is

    string Name;
    string FirstName;

    if i try to save a Name with longer a longer size i get the exception.

    - how can i get the possible size of a colum / field?  
    So that i can do some checks like = Name = value.SubString(0, sizeofColumn(Name))

    thanks for any tip in advance

  2. Answer
    Ralph Waldenmaier
    Ralph Waldenmaier avatar
    202 posts

    Posted 14 Mar 2014 Link to this post

    Hi remco,
    You can obtain the length of a particular column via the metadata.
    See the following example:
    var type = ((Telerik.OpenAccess.OpenAccessContextBase)(ctx)).Metadata.PersistentTypes.First(x => x.Name.Equals("YOURCLASSNAME"));
    var length = type.Table.Columns.First(x => x.Name.Equals("YOURCOLUMN")).Length;

    This will allow you to check the input values and avoid such errors.

    Feel free to ask in case you have any other question.

    Ralph Waldenmaier
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
Back to Top