Find maimum length of a varchar database column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    Find maimum length of a varchar database column

    I have a database which has a column of type VARCHAR(30). I want to insert a string into this column using ADO .net. I first want to make sure that the string will be able to fit into the column in the database. If it can fit, I will insert the whole string. If not, I will truncate the string so that it will fit.

    If I read the desired table into an SqlDataAdapter and then fill a dataTable, there is a property in dataTable.Colum ns.Item("Column ") called MaxLength, but it always returns -1.

    I want to use some method that will return 30. Any ideas?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Inserting a string that is too long will automatically truncate it. Unless you want special rules for truncating?

    Comment

    • Sick0Fant
      New Member
      • Feb 2008
      • 121

      #3
      Originally posted by Plater
      Inserting a string that is too long will automatically truncate it. Unless you want special rules for truncating?
      Then I guess I'd only need to know if it *was* truncated. Is there anyway of checking that without querying the db again to see if what was inserted is the same as what I tried to insert?

      Also, why does MaxLength return -1?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        A broad category of Microsoft tools, languages, and frameworks for software development. Designed to support developers in building, debugging, and deploying applications across various platforms.


        According to them, maxlength is -1 on integer fields, but should work fine for text fields(varchar( ) )

        Comment

        Working...