# .NET and More > Silverlight >  Create Unique Field

## mateo107

I've created a 'username' field that will be auto-completed based on first and last name in my form text fields, so it starts as Me.firstname + Me.lastname

What I'd like to do, is verify that no one else has that username first.  If they do, I want to loop through their first name, adding a character to the username until it's unique.

For example, lets say I enter:

John Smith... his username would be jsmith - check the database - no values so he gets jsmith

now I go to enter Jane Smith, but when the 'unique' check happens, she can't be jsmith, because there already is one... so it appends the second character to her name, she would be: jasmith.
if I entered June Smith, she would be jusmith, because jsmith is taken, but jusmith is not.

Now, if I wanted to enter Janice Smith, she would end up being jansmith, because jsmith is taken, it tries jasmith, thats taken, so it moves to jansmith.

Does this make sense?

How would I make this type of "recursive" check?

----------


## jmcilhinney

E.g.
vb.net Code:
Function CreateUserName(givenName As String, surname As String) As String    Dim substringLength = 1    Dim userName As String     Do        userName = givenName.Substring(0, substringLength) & surname        substringLength += 1    Loop Until IsUniqueUserName(userName)     Return userNameEnd Function
That handles what you've asked for but it will crash if it gets to the end of the given name without finding a unique user name.  You haven't told us what you want to do in that case so I haven't included it.  Hopefully it's obvious that you need to implement the IsUniqueUserName method yourself.

----------


## mateo107

thanks JM... most of this code works... one thing I didn't mention is that i'm doing this in ligthswitch - but VB.net within ligthswitch (silverlight application).

Everything seems to work except for the Loop Until isUniqueUsername
Perhaps this will help further things along.

To answer your previous question - I'm going to implement a counter and if it gets to the end and there still isn't a unique value, I will append the counter number to the end.

Table is called Users and the field it is checking again is called *****ername.  The form has FirstName and LastName fields, so heres the code I have so far, based on what you gave me.



```
            Dim substringLength = 1
            Dim userName As String

            Do
                userName = Me.FirstName.Substring(0, substringLength) & Me.LastName
                substringLength += 1
```

----------


## mateo107

and i'm not entirely sure how to iterate through the "Users".username table.fieldname - so i think that might be partially why this code isn't working.

So essentially, using your code and some psuedo-code, it should be if userName = FirstRecordinUsersTable.username - iterate the substringlength, start the search again, repeat until there is no match and return that value.  if no unique values hit after iterating through all substring lengths, make the username = userName & substringlength

----------


## jdc20181

You may need it moved to a silverlight/switch forum section:
http://www.vbforums.com/forumdisplay.php?88-Silverlight

Don't duplicate I will PM a Mod. to have it moved. They should be able to help a little easier there...

----------


## Shaggy Hiker

Thread moved to Silverlight forum. If there is a better place, just report the thread and I'll move it again (or somebody else will, whoever sees it first).

----------

