Click here to Skip to main content
11,705,212 members (42,741 online)
Click here to Skip to main content

Dynamically set TextBox.MaxLength based on SQL column size

, 25 Jan 2007 CPOL 31.4K 22
Rate this:
Please Sign up or sign in to vote.
Dynamically set TextBox.MaxLength based on the SQL column size.


Have you ever tried to update a varchar in a SQL Server table when the size of the string is longer than the column size? It does not work… How is it normally solved? You specify:

myEditBox.MaxLength = 100;

What if you SQL developer changes the column size but does not let you know?


public class SQLHelper
    static public int GetSize(string csConnectionString, string csTableName, 
                              string csColumnName, int iDefault)
        int iSize = iDefault;
        String csCommand =
            SqlConnection pConn = new SqlConnection(csConnectionString);

            SqlCommand pCommand = new SqlCommand(csCommand, pConn);
            pCommand.Parameters.AddWithValue("@TABLE_NAME", csTableName);
            pCommand.Parameters.AddWithValue("@COLUMN_NAME", csColumnName);
            SqlDataReader pReader = pCommand.ExecuteReader();
            if (pReader.Read())
                object pSize = pReader.GetValue(0);
                if (pSize != DBNull.Value)
                    iSize = Convert.ToInt32(pSize);
        catch (Exception em)
            MessageBox.Show(em.Message, "SQL Error");
        return iSize;

    static public int GetSize(string csConnectionString, 
                      string csTableName, string csColumnName)
        return GetSize(csConnectionString, csTableName, csColumnName, -1);


myTextBox.MaxLength = SQLHelper.GetSize(ConnectionString, TableName, ColumnName);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralUsing DataAdapter.FillSchema() Pin
loudenvier3-Jul-07 9:30
memberloudenvier3-Jul-07 9:30 
Generalan idea... Pin
ESTANNY25-Jan-07 10:26
memberESTANNY25-Jan-07 10:26 
GeneralRe: an idea... Pin
Alexandru Lungu25-Jan-07 11:47
memberAlexandru Lungu25-Jan-07 11:47 
GeneralRe: an idea... Pin
Fiwel25-Jan-07 12:26
memberFiwel25-Jan-07 12:26 
GeneralRe: an idea... Pin
DBuckner25-Jan-07 14:05
memberDBuckner25-Jan-07 14:05 
GeneralRe: an idea... Pin
Fiwel26-Jan-07 5:27
memberFiwel26-Jan-07 5:27 
GeneralRe: an idea... Pin
loudenvier3-Jul-07 8:00
memberloudenvier3-Jul-07 8:00 
GeneralRe: Using caching Pin
Jcmorin25-Jan-07 15:49
memberJcmorin25-Jan-07 15:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 25 Jan 2007
Article Copyright 2007 by Fiwel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid