I’ve got an asp:TextBox which has a max length of 50, through the MaxLength property. The contents of this text box is written to a database.
The problem is that if the user does not fill in all 50 characters, the rest of the text box is filled with white space – thus also being saved into the database like so.
Is there any way to combat this?
I’m going to go out on a limb here and suggest that you’ve declared your text column as
NCHAR(50)orCHAR(50)rather thanNVARCHAR(50)orVARCHAR(50).The former will write 50 characters to the table – padding out the input with spaces if necessary. The latter will write up to 50 characters to the table.
If you make this change then any new data will be written without trailing spaces, but any existing data will have to be cleaned up: