"Chris" <gd***@er.dfwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
When the length of a textbox in a form exceeds the length of the
corresponding field, i get an error message ("string or binary data will
be truncated .... ") and the aplication stops.
I know i can check the lenght in code-behind, but is there no way to let
asp.net truncate inputted data automatically and not let it stop the
application and show that error message?
A couple of solutions:
Firstly, the <asp:TextBoxcontrol has a MaxLength property. If e.g. your
database field is varchar(50), setting the MaxLength also to 50 will prevent
your users from typing more text than will fit in the database.
Secondly, you can return the first n characters from a TextBox control like:
TextBox.Text..Substring(0, 50);
The first option is preferable, IMO. If you allow users to enter more data
than the database can accept, next time they visit the record they may
believe that an error has occurred...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net