Click here to Skip to main content
Licence CPOL
First Posted 25 Jan 2007
Views 25,423
Bookmarked 20 times

Dynamically set TextBox.MaxLength based on SQL column size

By | 25 Jan 2007 | Article
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:

myEditBox.MaxLength = 100;

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

Solution

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

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)

About the Author

Fiwel

Software Developer (Senior)

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUsing DataAdapter.FillSchema() Pinmemberloudenvier9:30 3 Jul '07  
Generalan idea... PinmemberESTANNY10:26 25 Jan '07  
GeneralRe: an idea... PinmemberAlexandru Lungu11:47 25 Jan '07  
GeneralRe: an idea... PinmemberFiwel12:26 25 Jan '07  
GeneralRe: an idea... PinmemberDBuckner14:05 25 Jan '07  
GeneralRe: an idea... PinmemberFiwel5:27 26 Jan '07  
GeneralRe: an idea... Pinmemberloudenvier8:00 3 Jul '07  
GeneralRe: Using caching PinmemberJcmorin15:49 25 Jan '07  

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.

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