Click here to Skip to main content
Licence CPOL
First Posted 6 Jan 2008
Views 11,315
Bookmarked 16 times

How to set the maximum allowed length of textboxes dynamically

By | 6 Jan 2008 | Article
How to set the maximum allowed length of textboxes dynamically.
 
Part of The SQL Zone sponsored by
See Also

Introduction

I remember how annoying it was in Visual Basic 6 to manually set the maximum allowed length of textboxes according to the maximum length of the table column where the data was going to be stored.

Now, when working with the Microsoft .NET Framework, we have ways to dynamically set the maximum allowed length of textboxes.

In this article, we are going to see how to set the maximum allowed length by getting the correct value from the DataTable where the data is stored.

Background

We need to obtain the schema information of the table to know the maximum allowed length of the data column. So, we are going to use a DataAdapter property called MissingSchemaAction. For more information on this property, refer to this page: http://msdn2.microsoft.com/en-us/library/49z48hxc.aspx.

Using the Code

First of all, we need to get the data (I assume that the connection is opened elsewhere):

public DataTable GetData(SqlConnection oConnection,string SqlQuery)
{
    SqlCommand oCommand;
    SqlDataAdapter oDataAdapter;
    DataTable dtData=null;
    oCommand = new SqlCommand(SqlQuery, oConnection);
    oDataAdapter = new SqlDataAdapter(oCommand);
    //we need this to obtain the schema data of the table
    oDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    oDataAdapter.Fill(dtData);
    oDataAdapter.Dispose();
    oCommand.Dispose();
    return dtData;
}

The textboxes added in the form have to be named like the columns of the table, so we can find them according to the DataTable column names.

private void SetMaxLengthText()
{
    foreach (DataColumn dc in dtDatos.Columns)
    {
        Control[] control;
        control = this.Controls.Find(dc.ColumnName, true);
        if (control.Length > 0)
            ((TextBox)control[0]).MaxLength = dc.MaxLength;
    }
}

License

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

About the Author

Santiago Sanchez

Software Developer

Spain Spain

Member

I´ve been working with Oracle, Sql Server and Visual Basic 6 since 2003 and with C# since 2006. Now I´m fighting with Biztalk 2006 too...
 
MCTS .NET Framework 4, Windows Applications

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
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 6 Jan 2008
Article Copyright 2008 by Santiago Sanchez
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid