Link to home
Start Free TrialLog in
Avatar of MTecho
MTecho

asked on

MaxLength for varchar column

To store a string in a varchar field the maximum length of string is needed to avoid a data-truncation error.

The following code only returns -1 if the column is varchar or ntext:

dr.Table.Columns(Ind).MaxLength

where Ind is the index of the required column.

dr.Table.Columns(Ind).DataType.ToString only returns "System.String", if the column is varchar or ntext.

What is the equivalent of the VB6 Fd.DefinedSize (where Fd is ADODB.Field)?
Avatar of graye
graye
Flag of United States of America image

By default, ADO.Net the DataAdapter's Fill() method takes a "short cut" and only pulls the bare bones information required to populate the DataTable.  

To get the fully populated schema (which includes the column widths) just add the following:

           DataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

Avatar of MTecho
MTecho

ASKER

Thank you for that, Graye.

However, dr.Table.Columns(Ind).DataType.ToString still returns "System.String", if the column is varchar or ntext.

Is it possible to get the Data Type from the table in the SQL database?
ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial