views:

41

answers:

4

Hi

I am trying to remove all spaces from a text field when a form is submitted so that the contents of the post code field match those in a database...

If drSet5c.Tables(0).Rows(0).Item("post_code") = UCase(Replace(tbPostcode.Text, " ","")) Then
    response.write("Postcodes match")
Else
    response.write("Postcodes DON'T match")
End If

So if the postcode in the database is AB12CD and the user types AB1 2CD the space is removed on submitting the form and the match statement shows. This is not working though.

It all works fine if the user does not include the space.

Any ideas?

A: 

Try to use not just Replace() but String.Replace()

abatishchev
And how do I go about doing that?
Tom
@Tom: Sorry, elaborate your questions please.
abatishchev
+1  A: 

That's because Ucase is a static (VB Shared) method while Replace is an instance method. Try this:

tbPostcode.Text.Replace(" ","").ToUpper
Xaqron
This doesn't break it which is good, but it still returns the exact same result.
Tom
+1  A: 

Create a temporary Console application and run this code:

Module Module1
    Sub Main()
        Dim dbaseValue = "AB12CD"
        Dim userInput = "AB1 2CD"
        If UCase(dbaseValue) <> UCase(Replace(userInput, " ", "")) Then
            Console.WriteLine("You need a new machine")
        Else
            Console.WriteLine("The bug is elsewhere")
        End If
        Console.ReadLine()
    End Sub
End Module

What does it say?

Hans Passant
+1  A: 

Try breaking it up into its parts so you can evaluate the string properly. Doing all of this on one line is less readable and more prone to error. Also, get away from using the old notation of VB6 with UCase, CStr, Len, etc. They're still valid within the Microsoft.VisualBasic namespace (and MS assures us that it's not going anywhere), but you'll find example code easier to translate if you just familiarize yourself with the .Net object structure.

' Get the row string out using ToString to force the typing
Dim rowPostCode As String = drSet5c.Tables(0).Rows(0).Item("post_code").ToString()

' Perform the case manipulation and replace function, then assign to new string
Dim tbPostCodeFormatted As String = tbPostcode.Text.Trim().ToUpper().Replace(" ", "")

' Perform the string comparison. Because we're ignoring case here, the ToUpper()
' used above may not be necessary
If rowPostCode.Equals(tbPostCodeFormatted, StringComparison.OrdinalIgnoreCase) Then 
    response.write("Postcodes match") 
Else 
    response.write("Postcodes DON'T match") 
End If
Joel Etherton
Thanks for this Joel. Still doesn't fix the problem but some good advice. I'm picking up from where someone else left off and learning as I go along so all the help is appreciated.
Tom
@Tom - While you're in debug mode, you should be able to put the mouse over the individual variables rowPostCode and tbPostCodeFormatted to view them independently to see if they are identical. If they are not, can you post what they are so we can see what's going on? It's entirely possible that you have whitespace in one of the fields that is not specifically a `' '`.
Joel Etherton
@Joel - I see what you're saying but if I type the postcode into the box without the space it works fine. This would suggest it's doing the comparison before I remove the space right?
Tom
@Tom - or that it's not modifying the compared variable for whatever reason. That's why I recommended dumping them into separate individual variables before performing the comparison. As @Xaqron mentioned, Replace is an instance function. What he didn't elaborate on is that it's a function. You can't just call replace on an object and expect it to "stick". Replace returns the result, so if you don't store the result somewhere, you haven't done anything. That's why in my code sample, I store it in a separate variable unrelated to the textbox itself. This allows replace to function normally.
Joel Etherton
There was a problem elsewhere on the page causing the problem put I did implement your advice so I'm going to give you the tick.
Tom