asp.net do until loop




asp.net do until loop
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:
  1. Dim strLoopStatus As String = 0   
  2. Do Until strLoopStatus = 1   
  3.          'Action/check info goes here   
  4. Loop  
An example of this in action would be:
  1. Dim MyConnection As SqlConnection   
  2.         MyConnection = New SqlConnection()   
  3.         MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString   
  4.   
  5. Dim strUsername As String = txtFirstName.Text &txtLastName.Text   
  6. Dim strUsername2 As String = txtFirstName.Text & txtLastName.Text   
  7. Dim strLoopStatus As String = 0   
  8. Dim strLoopCount As String = 0   
  9.   
  10. Do Until strLoopStatus = 1   
  11.        Dim sql As String = "SELECT id FROM TABLE WHERE Username = '" + strUsername + "'"  
  12.        Dim resultsDataSet As New DataSet()   
  13.        Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(sql, MyConnection)   
  14.        myDataAdapter.Fill(resultsDataSet, "usernames")   
  15.   
  16.        If resultsDataSet.Tables("usernames").Rows.Count = 0 Then  
  17.               strLoopStatus = 1   
  18.        Else  
  19.               strLoopCount = CStr(strLoopCount + 1)   
  20.               strUsername = strUsername2 & strLoopCount   
  21.        End If  
  22. Loop  
  23.   
  24. 'The output you would then use is strUsername which will have been checked to be unique and available  
 


No one has said that they like article yet, be the first and click here (0)
Send a link to this page via email Send a link to this page