Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Trimming a textbox value

When i bind my textbox to a sql datasource field as so

<asp:TextBox ID="tbContact" runat="server" Text='<%# Bind("contact") %>' ></asp:TextBox>

How could i either field trim the value before it gets bound with the sqldatasource selectcommand OR when after the information is entered and upadted with the updatecommand

<asp:SqlDataSource ID="id" runat="server" ConnectionString="<%$ ConnectionString %>"

SelectCommand="SELECT [[contact] FROM [table]

UpdateCommand="UPDATE [table] SET [contact] = @contact WHERE [key] = @key">

any ides?
[629 byte] By [clean] at [2007-11-11 8:08:50]
# 1 Re: Trimming a textbox value
Try changing your UpdateCommand to:

"UPDATE [table] SET [contact] = LTRIM(RTRIM(@contact)) WHERE [key] = @key"
Phil Weber at 2007-11-11 23:13:23 >
# 2 Re: Trimming a textbox value
thanks for the reply i tried that on the select statement instead and it does not seem to work it throws an error not recognizing the field when binding

<asp:TextBox ID="tbContact" runat="server" Text='<%# Bind("contact") %>' ></asp:TextBox>

Error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'contact'.

SelectCommand="SELECT LTrim(RTrim([contact])) As contact FROM [table]"
clean at 2007-11-11 23:14:23 >
# 3 Re: Trimming a textbox value
Uh, yeah, that's not what I suggested. You asked how you could trim the value in the SelectCommand OR the UpdateCommand; I told you how to do it in the UpdateCommand. Why did you change the SelectCommand? ;-)
Phil Weber at 2007-11-11 23:15:27 >
# 4 Re: Trimming a textbox value
well i thought it would work the same way oviously it did not.

Is there a way to do it in the selectcommand as well.

In the updatecommand i think my validationcontrols will take priority over the the updatecommand so it will not really matter there to much, but it will do be good for the control that do not have any validationcontrol tied to them
clean at 2007-11-11 23:16:32 >
# 5 Re: Trimming a textbox value
I really don't know, I haven't worked with ASP.NET 2.0's declarative data binding. Start Googling! ;-)
Phil Weber at 2007-11-11 23:17:27 >