asp.net do until loop
Monday 10 August 2009
If you need to loop through a process, say checking if a username is available and suggesting an alternative, you can achieve this quite simply by using the below code:
- Dim strLoopStatus As String = 0
- Do Until strLoopStatus = 1
- 'Action/check info goes here
- Loop
An example of this in action would be:
- Dim MyConnection As SqlConnection
- MyConnection = New SqlConnection()
- MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
-
- Dim strUsername As String = txtFirstName.Text &txtLastName.Text
- Dim strUsername2 As String = txtFirstName.Text & txtLastName.Text
- Dim strLoopStatus As String = 0
- Dim strLoopCount As String = 0
-
- Do Until strLoopStatus = 1
- Dim sql As String = "SELECT id FROM TABLE WHERE Username = '" + strUsername + "'"
- Dim resultsDataSet As New DataSet()
- Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(sql, MyConnection)
- myDataAdapter.Fill(resultsDataSet, "usernames")
-
- If resultsDataSet.Tables("usernames").Rows.Count = 0 Then
- strLoopStatus = 1
- Else
- strLoopCount = CStr(strLoopCount + 1)
- strUsername = strUsername2 & strLoopCount
- End If
- Loop
-
- 'The output you would then use is strUsername which will have been checked to be unique and available
|
(11)
|
|
|