Showing posts with label space. Show all posts
Showing posts with label space. Show all posts

Saturday, March 31, 2012

Trim function in VB.NET

Hey,

In my code the value of strWebService is initially "2 " (that is the number two followed by a space). I then use trim to eliminate the space:


strWebService = readHtmlPage(strTMQuery)
strWebService = Trim(strWebService)

I then run strWebService through a SELECT statement and it always defaults to ELSE:

Select Case strWebService
Case "0"
pnlLogin.Visible = False
pnlPassengerList.Visible = True
Case "1"
lblLoginError.Text = "Password is wrong...<br /><br />"
lblLoginError.Visible = True
Case "2"
lblLoginError.Text = "E-mail address incorrect or not a valid user...<br /><br />"
lblLoginError.Visible = True
Case Else
lblLoginError.Text = "Unknown Error: " & strWebService & "<br /><br />"
lblLoginError.Visible = True
End Select

What am I doing wrong?

MikeHello -

Perhaps you might want to do a conversion to an integer for the case statement?

good luck

take care
tony
I added the following line before my select statement:

Dim intWebService As Integer = CInt(strWebService)

I then removed the double quotes from my case expressions and ran the code but still got the ELSE statement. VB doesn't require BREAK does it?
did you trying doing a response.Write(intWebService) right before the case statement so you can verify the value?


response.write("service =" & strWebService & ".")

Take note of the spaces and period. Should be
service =2.
My code now reads like this (I added the = signs to the label just to make sure the TRIM did its job...)


strWebService = readHtmlPage(strTMQuery)
strWebService = RTrim(strWebService)
Dim intWebService As Integer = CInt(strWebService)

lblTest.Text = "=" & intWebService.ToString & "="
lblTest.Visible = True

Select Case strWebService
Case 0
pnlLogin.Visible = False
pnlPassengerList.Visible = True
Case 1
lblLoginError.Text = "Password is wrong...<br /><br />"
lblLoginError.Visible = True
Case 2
lblLoginError.Text = "E-mail address incorrect or not a valid user...<br /><br />"
lblLoginError.Visible = True
Case Else
lblLoginError.Text = "Unknown Error: " & strWebService & "<br /><br />"
lblLoginError.Visible = True
End Select

I still get the ELSE... Plus, my label does infact read 2... I don't get what I could be doing wrong...
Your select Statement is still referring to strWebService... Change it to intWebService...
Ha ha, good call. It works. Thanks guys. Strings in SELECT CASE statements SUCK!

Trim TextBox spaces

I am facing difficulity of having to Trim Spaces in Textbox.I am given a task of needing to trim spaces in the textbox and not to allow space at the front and at the back of the textbox as when u enter a space in front of the textbox it will be a totally different data from the one which has no space. So can anyone help me about the trim in Asp.net 2.0 c# languague.

<trclass="i4">

<tdclass="i5">

<span><span><spanclass="i6">C</span>USTCODE</span><spanclass="i6">

</span>

</span>

</td>

<tdclass="i7">

<asp:RequiredFieldValidatorID="RequiredFieldValidator2"runat="server"ControlToValidate="CUSTCODETextBox"

ErrorMessage="*"Font-Bold="False"></asp:RequiredFieldValidator></td>

<tdcolspan="2"class="i8">

<asp:TextBoxID="CUSTCODETextBox"runat="server"Text='<%# Bind("CUSTCODE") %>'MaxLength="20"></asp:TextBox></td>

<tdrowspan="10"class="i9"></td>

When you are saving the data in your code behind, you can trim the text using this code:

string customerCode = CUSTOMCODETextBox.Text.Trim();

That will get you the value entered by the user, without any leading or trailing spaces.