how to truncate automatically?

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
 
M

Mark Rae [MVP]

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...
 
A

Alex Meleta

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
 
R

Riki

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.
 
M

Masudur

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
 
R

Riki

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top