Click here to Skip to main content
15,881,870 members
Articles / Programming Languages / C#

Dynamically set TextBox.MaxLength based on SQL column size

Rate me:
Please Sign up or sign in to vote.
3.25/5 (4 votes)
25 Jan 2007CPOL 45.2K   22   8
Dynamically set TextBox.MaxLength based on the SQL column size.

Introduction

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:

C#
myEditBox.MaxLength = 100;

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

Solution

C#
public class SQLHelper
{
    static public int GetSize(string csConnectionString, string csTableName, 
                              string csColumnName, int iDefault)
    {
        int iSize = iDefault;
        String csCommand =
            @"select CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS 
                WHERE TABLE_NAME = @TABLE_NAME AND COLUMN_NAME = @COLUMN_NAME";
        try
        {
            SqlConnection pConn = new SqlConnection(csConnectionString);
            pConn.Open();

            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);
                }
            }
            pReader.Close();
            pConn.Close();
        }
        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);
    }
}

Usage

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

License

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


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUsing DataAdapter.FillSchema() Pin
Felipe R. Machado3-Jul-07 9:30
professionalFelipe R. Machado3-Jul-07 9:30 
Generalan idea... Pin
ESTAN25-Jan-07 10:26
ESTAN25-Jan-07 10:26 
GeneralRe: an idea... Pin
Alexandru Lungu25-Jan-07 11:47
professionalAlexandru Lungu25-Jan-07 11:47 
GeneralRe: an idea... Pin
Fiwel25-Jan-07 12:26
Fiwel25-Jan-07 12:26 
GeneralRe: an idea... Pin
DBuckner25-Jan-07 14:05
DBuckner25-Jan-07 14:05 
GeneralRe: an idea... Pin
Fiwel26-Jan-07 5:27
Fiwel26-Jan-07 5:27 
GeneralRe: an idea... Pin
Felipe R. Machado3-Jul-07 8:00
professionalFelipe R. Machado3-Jul-07 8:00 
GeneralRe: Using caching Pin
Jcmorin25-Jan-07 15:49
Jcmorin25-Jan-07 15:49 

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

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