how to truncate automatically?

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,

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?

Thanks
Chris
 
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:TextBox> control 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...
 
Hi Chris,

You should check a length of input (form) string and set the length constrain
in a sql parameter of sql command, like SqlParameter("@value", SqlDbType.VarChar,
<set_cetrain_size_there>).

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
 
Chris said:
Hi,

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?

The best way in my opinion is to set the maxlength property on the textbox.
 
Hi,

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?

Thanks
Chris

Hi...

I am not quite sure that this will help but still, please do checkout
this forum post...
http://forums.asp.net/p/436606/437849.aspx

Thanks
Md. Masudur Rahman (Munna)
kaz Software Ltd.
www.kaz.com.bd
http://munnacs.110mb.com
 
Chris said:
Thanks to all,

one remark: Maxlength doesn't work when mode is Multiline ...

In that case, another option, not yet mentioned, is to use a validator
(Regular Expression Validator) on the textbox.
 
Back
Top