 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 17th, 2005, 11:28 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
databinding to a textbox, whitespace problem
Is there a way to remove the whitespace from a databound textbox field besides calling RTRIM() in the SQL Select Statement?
Thanks
|
|

January 17th, 2005, 03:52 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Where does the white space occur?
Are you trying to get rid of any whitespace your user has entered? Or do you get unwanted whitespace when you display the information?
In the first case, you can use Trim() on the Text property of the TextBox control to strip the whitespace in your C# code, otherwise you may need to take a look at the way the control is added to the page. Maybe there is whitespace around the place where you do your databinding....
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

January 18th, 2005, 11:42 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i am retrieving data from a table, the type is a char. Supposed I stored the word 'WROX' into my char field of length 10. When I bind that data to a text box, it will show WROX------ (dashes indicated whitespace) this becomes a problem if i set the textbox field to 10-changes can't be made unless you remove the whitespace.
I've decided to add RTRIM in my select statement, however if there is an easier way i would appreciate it
|
|

January 18th, 2005, 12:46 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Ah, that explains a lot.
The length of the text in a Char field will be the size you defined for the Char. So, a Char(10) always has a length of 10, regardless of the actual size.
What you need (in the database) is a VarChar. A VarChar(10) has a maximum size of 10; yet if you store Wrox in it, it will have a length of 4, and no additional whitespace is added.
Cheers,
Imar
|
|

January 18th, 2005, 04:22 PM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
If this is a WinForms application then you can use the Format event of the Binding object to do the trimming. This event is fired whenever the value read from the database is about to be displayed. For example, in the form's constructor include the code:
Binding b = textBox1.DataBindings["Text"];
b.Format += new ConvertEventHandler(TrimName);
and then the actual handler would be:
private void TrimName(object sender, ConvertEventArgs cevent) {
cevent.Value = ((string)cevent.Value).Trim();
}
I'm not sure if there's anything similar for an ASP.NET application (I don't know much about ASP.NET), but I'd have thought there would be.
Chris Jobson
Chris Jobson
|
|

January 19th, 2005, 02:39 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
In ASP.NET you could use a custom function in the databinding expression, or directly call Trim.
For template based data bound controls, like the Repeater and the DataGrid you can use the ItemCreated event to get at the text item, trim it and then return it.
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|
 |