-1

I have this code where i want this for loop to iterate for each row. But this code get do not iterate when the if condition is false.
I tried "continue" in else part.But it did not work. Even though if i place a msgbox in else part and click msg box at each time it display then the loop continue. but it is not practical as I have 1000s of rows to check on.

Public Function SMS()
    CustomerTableAdapter.Fill(MyHotelManagementSystemDataSet63.Customer)
    For i = 0 To MyHotelManagementSystemDataSet63.Customer.Rows.Count - 1
        If MyHotelManagementSystemDataSet63.Customer(i).DOB.Day = Date.Now.Day AndAlso _
            MyHotelManagementSystemDataSet63.Customer(i).DOB.Date.Month = Date.Now.Month Then
            Dim SerialPort As New System.IO.Ports.SerialPort()
            If SerialPort.IsOpen Then
                SerialPort.Close()
            End If
            SerialPort.PortName = "COM29"
            SerialPort.BaudRate = 9600
            SerialPort.Parity = Parity.None
            SerialPort.StopBits = StopBits.One
            SerialPort.DataBits = 8
            SerialPort.Handshake = Handshake.RequestToSend
            SerialPort.DtrEnable = True
            SerialPort.RtsEnable = True
            SerialPort.NewLine = vbCrLf
            Dim message As String
            Dim nm = MyHotelManagementSystemDataSet63.Customer(i).Name
            Dim tp = MyHotelManagementSystemDataSet63.Customer(i).Telephone
            message = "Dear " + nm + " ***)"
            SerialPort.Open()
            If SerialPort.IsOpen() Then
                SerialPort.Write("AT" & vbCrLf)
                SerialPort.Write("AT+CMGF=1" & vbCrLf)
                SerialPort.Write("AT+CMGS=" & Chr(34) & tp & Chr(34) & vbCrLf)
                SerialPort.Write(message & Chr(26))
                SerialPort.Close()
            Else
                MsgBox("Port not available")
            End If
        Else

        End If
    Next
    Return True
End Function

Pleaes help me to make this for loop continue without any user interaction.

Fabio
  • 31,528
  • 4
  • 33
  • 72
A1990
  • 63
  • 10
  • 2
    Your question is not clear. What do you mean by saying "do not interate"? why? – Sateesh Pagolu Oct 29 '15 at 10:54
  • Actually the loop continiues automatically if `i >= 2`. You don't need to call `Continue` explicitly. – Tim Schmelter Oct 29 '15 at 10:54
  • The loop iterates from `0 To CustomerDataSet.customer.Rows.count - 1`, but because of your `If` only the values `0` & `1` "Do somethin'". – Enigmativity Oct 29 '15 at 10:55
  • @ Tim Schmelter this check 1st line of the code and if the "if" condition met loop continues to the second line. but if the "If" condition not met loop does not move to next row? – A1990 Oct 29 '15 at 11:42
  • @Dark Knight It is not interate,it is iterate – A1990 Oct 29 '15 at 12:11
  • Your code is ok. You need to check your data, is it possible all rows satisfied your condition? And use `AndAlso` instead of `And`. `And` is bitwise operator [What is the difference between And and AndAlso in VB.net?](http://stackoverflow.com/questions/302047/what-is-the-difference-between-and-and-andalso-in-vb-net) – Fabio Oct 29 '15 at 12:25
  • @Fabio: That is the issue, Not all rows satisfy condition. And when ever there come a row that do not satisfy condition it just do not move ahead. – A1990 Oct 29 '15 at 13:00
  • What you mean _do not move ahead_? If it is not last row and if code inside doesn't contain `Exit For` it will iterate next row. Another possibility if your `For.. Next` loop is inside `Try Catch` block which do nothing on the `Catch`. Then it swallow possible error exception inside loop and continue executing – Fabio Oct 29 '15 at 14:49
  • @Fabio : Say there is a row, and it is not the last row,but it does not satisfy the If condition. Therefore loop does not continue.If I add "Else MsgBox("Something") End If " then the loop continue when I click MsgBox – A1990 Oct 29 '15 at 15:02
  • Show your full function where `For Next` loop is. How you check that iterating continue or not? – Fabio Oct 29 '15 at 15:13
  • @Fabio :Edited, I figured out Iteration does not happen because I do not get an sms for my next possible row . – A1990 Oct 29 '15 at 15:25

2 Answers2

0

Have you checked if the Rows.Count is set and isn't 0

If that isn't the case

For i = 0 To CustomerDataSet.customer.Rows.count - 1 
  if i < 2 Then 
    'Do something
  Else
    Continue For
  End If
Next
DieVeenman
  • 457
  • 1
  • 3
  • 18
  • Veenman Row count is not set and As I mentioned earlier,if i set a msg box in else part and click it at each time it appears then loop continues. But what I want is ti have something in else part that direct execution to next row, what should have for the else section?? please help – A1990 Oct 29 '15 at 11:45
  • @A1990 Edit: As soon as the code enters the Else block it will go to the next Iteration of the For loop (e.g. if I = 2 it will go to the 4th iteration(i = 3 then)) I think this is what you're searching for – DieVeenman Oct 29 '15 at 11:55
  • 1
    Execution will continue to the next row without any code in the else statement – Fabio Oct 29 '15 at 11:55
  • @Fabio : No unfortunately it is not happening – A1990 Oct 29 '15 at 11:59
  • @A1990 is there any other relevant code and could you put a breakpoint on `For i = 0 To CustomerDataSet.customer.Rows.count - 1` – DieVeenman Oct 29 '15 at 12:00
  • @A1990, Please update your question with new code. Do not put it in the comment - no format, difficult to read and understand – Fabio Oct 29 '15 at 12:07
0

Use Exit For if you don't want iterate rows after condition return false

For i = 0 To CustomerDataSet.customer.Rows.count - 1
    If condition = false Then Exit For
    'Do something for rows which condition = true
Next

Use Continue For if you want iterate to the next row without executing your DoSomething code

For i = 0 To CustomerDataSet.customer.Rows.count - 1
    If condition = false Then Continue For
    'Do something for rows which condition = true
Next
Fabio
  • 31,528
  • 4
  • 33
  • 72