Getting VarChar sizes using vb.net

Discussion in 'Databases' started by Doxasta, Apr 6, 2005.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hi,

    I can't seem to find a way of getting the varchar sizes set in MS SQL tables using VB.NET


    I've tried using...

    Table.Columns(0).MaxLength

    But always get -1 regardless of what datatype the column is.

    I need this figure to prevent users from trying to enter to much data into a varchar field.

    Any ideas



    Simon
     
  2. Bruce

    Bruce DiscountASP.NET Staff

  3. Hi Bruce,





    Posting the whole code would be complicated as I'm using functions to get a working


    System.Data.DataTable
    from this I'm picking up on the columns in the table using




    Dim Col As System.Data.DataColumn


    dim s as integer
    for each Col in Table.Columns
    s = col.maxlength
    next

    I always get -1 back. In the end I used a function to query the database...




    Dim sql As String = "SELECT CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS "


    sql += "WHERE TABLE_NAME = '" & Tablename & "' AND COLUMN_NAME = '" & ColumnName & "'"


    It would be simpler using col.maxlength tho.


    I was using sqlclient classes to get the table.





    Simon
     
  4. Bruce

    Bruce DiscountASP.NET Staff

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page