Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Dynamically limit the length of data entered in a textbo
Message
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
ASP.NET
OS:
Windows 2000 SP4
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01027460
Message ID:
01029991
Views:
14
This message has been marked as the solution to the initial question of the thread.
>In ASP 3.0 you would use code such as the example below to dynamically limit the data a user could enter into a text box based upon the length of a field.
>
>
>< input style="HEIGHT: 20px; LEFT: 110px; POSITION: absolute;
>               TOP: 5px; WIDTH: 100px; FONT-SIZE: 9pt;"
>               id=txtQCEContact
>               name=txtQCEContact
>               value=" <%= oRS(QCEContact)   "
>               ReadOnly
>               maxlength=" <%=oRS(QCEContact).DefineSize   “>
>
>
>
>In my ASP.NET application I would like to use something similar. I do not want to hard code the field length as field size changes all too often. Without limiting the maximum field length if your user enters too many characters or numbers you will receive an Error:
>
>String or binary data would be truncated. The statement has been terminated.
>
>Does anyone have a solution?
>
>Tom

This works

Creating dynamic field length

If the size of a field changes the maximum data allowed will be adjusted accordingly.

Create a DataTable as a Data Class

Use varchar data type to allow the cursor to enter the textbox.
public static DataTable FieldLength()
{
string selectStatement = "SELECT * "
+ "FROM UserInfo WHERE 1 = 2 ";
SqlCommand selectCommand = new SqlCommand(selectStatement,ScrapConnection.GetConnection());
SqlDataAdapter userDataAdapter = new SqlDataAdapter(selectCommand);
DataSet userDataSet = new DataSet();

userDataAdapter.FillSchema(userDataSet, SchemaType.Source, "UserInfo");

ScrapConnection.GetConnection().Close();
			
return userDataSet.Tables["UserInfo"];			
}
In the forms Load
string FirstName;
FirstName = UsersDB.FieldLength().Columns["FirstName"].MaxLength.ToString();
txtFirstName.MaxLength = Convert.ToInt32(FirstName);

string LastName;
LastName = UsersDB.FieldLength().Columns["LastName"].MaxLength.ToString();
txtLastName.MaxLength = Convert.ToInt32(LastName);
I am sure that there are other solutions but this works for now.
Previous
Reply
Map
View

Click here to load this message in the networking platform