Solved

VB.Net - Conversion from VB6 with GoTo

Posted on 2013-01-23
14
296 Views
Last Modified: 2013-01-28
Good Day Experts!

I have run into another issue with my little conversion project from VB6 to VB.Net.
The original VB6 code is run every Friday and produces reports accurately.  However, the speed is the issue and the reason for the conversion to VB.Net.  

Ok, one of the routines has a select query at the top of it that returns a count.  If that count is 0 then control goes to a LABEL: further down in the code.  That LABEL: is inside of a DO LOOP.  

After converting the code to .Net,  the line of code that performs the GoTo LABEL: has an error under the LABEL: that reads --> GoTo 'DoNextClient' is not valid because 'DoNextClient' is inside a 'For' or 'For Each' statement that does not contain this statement.

The above referenced DO LOOP was converted to a FOR EACH loop which is referenced in the error.

I understand that the use of GoTo statements is not desireable.  But due to time constraints and complexity of the code I am not is a position to rewrite the logic.  

Do you know of a way I can recitify the error and still be able reference that LABEL: in the FOR EACH loop?

Thanks for the help,
jimbo99999
0
Comment
Question by:Jimbo99999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 166 total points
ID: 38810444
You won't be able to use a For Each, I believe, because there is an iterator object that is created within the initialization of the For Each that I suspect would not occur if you used a GOTO. Is there a reason not to continue with the DO loop?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38810549
Can you show some code? Is that loop just making the program wait till the count is greater than 0?
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 84 total points
ID: 38810661
I agree.  Probably should change to a While type loop.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Jimbo99999
ID: 38810674
Thanks for responding.

I converted  the Recordset to a DataTable and used the ForEach to iterate through it.
I am not opposed to a Do Loop if there is a way I can use it with my DataTable.  
Can I use a Do Loop with my DataTable?

Thanks,
jimbo99999
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38811014
I'll preface this by saying that I'm not terribly fond of the approach, and I think it may end up being simpler to refactor the code, but you might try something along the lines of:

Dim i As Nullable(Of Integer) = Nothing

Do

DoNextClient:
    If i Is Nothing Then
        i = 0
    End If

    i += 1

    Dim row As DataRow = _theDataTable.Rows(i)

    ' do something with row

    ' Other logic
While something = True

Open in new window

0
 

Author Comment

by:Jimbo99999
ID: 38811822
Thanks for responding and patience as I had a fire had to be put out.

Here is the basic code from the VB6 version.  At the bottom, seperated by a line of *** I have my curent VB.Net version.

Set sqlRS3 = SQLconn2.Execute("SELECT count(*) as mycount FROM REPORTING.dbo.[PROCESSING] (NOLOCK)  WHERE [ACCOUNT NUMBER]='" & strAcct & "' AND [PROCESS WEEK]='" & strProcWeek & "' and DONE=1 ")

If sqlRS3("mycount") = 0 Then GoTo DoNextClient


Do Until sqlRS.EOF = True
....

DoNextClient:
    sqlRS.MoveNext
Loop
*************************************************
cmdSqlRS3 = New SqlCommand("SELECT count(*) as mycount FROM REPORTING.dbo.[PROCESSING] (NOLOCK)  WHERE [ACCOUNT NUMBER]='" & strAcct & "' AND [PROCESS WEEK]='" & strProcWeek & "' and DONE=1 ", SQLConn2)
rdrSqlRS3 = cmdSqlRS3.ExecuteReader
 Dim HasRows As Integer = 0
 If rdrSqlRS3.HasRows = True Then
           HasRows = rdrSqlRS3.GetValue("mycount").Value
 End If
 cmdSqlRS3.Dispose()
 rdrSqlRS3.Close()
 If HasRows = 0 Then GoTo DoNextClient

For Each cmdSqlRSDataRow As DataRow In sqlRStbl.Rows

DoNextClient:
Next
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38812205
It would make more sense to rewrite this code. I do not know exactly what this code is doing but it looks like its doing some processing based on count in another recordset. I think a simple loop or a nested loop on a datatable may work. Can you describe, in functional terms, what this code is doing?
0
 
LVL 3

Expert Comment

by:contactnaeem
ID: 38812630
Your above vb6 code is not complete and not sure if logic is correct here, please post completet code to get more accurate answer, meanwhile I have converted your vb6 code to best of my understanding.

Dim  cmdSqlRS3 as SqlCommand= New SqlCommand("SELECT count(*) as mycount FROM REPORTING.dbo.[PROCESSING] (NOLOCK)  WHERE [ACCOUNT NUMBER]='" & strAcct & "' AND [PROCESS WEEK]='" & strProcWeek & "' and DONE=1 ", SQLConn2))
        Dim rdrSqlRS3 As SqlDataReader = cmdSqlRS3.ExecuteReader

        While (rdrSqlRS3.Read())
            'here you can perform what ever your logic says in vb6 ---
            'you can read current row cols like below
            'rdrSqlRS3.GetString(0);
        End While
0
 

Author Comment

by:Jimbo99999
ID: 38814065
The Do Until Loop from the original VB6 code is a different Recordset than the one that gets the mycount.  That is what the issue is when I convert to VB.Net...it wont let me "GoTo" a label that is inside a For Each loop.
0
 

Author Comment

by:Jimbo99999
ID: 38814317
CodeCruiser:
I went through the code initially and converted all the Recordsets to either DataReaders or DataTables.

As you suggested, I reviewed the code here this morning is attempts to provide a description of what it is doing for you.  In all honesty,  I am not sure what is going on from a logical perspective.  I can follow it code wise but have no idea the "whys" of what is going on. The author of the code will be back in tomm and I will be able to get more information.

Thanks,
jimbo99999
0
 
LVL 3

Assisted Solution

by:contactnaeem
contactnaeem earned 84 total points
ID: 38815212
Above i have converted do until loop in a while loop and removed foreach loop, We don't need a goto label here.

If you can provide complete file or atleast complete method then I will be in a better position to convert it.
0
 

Author Comment

by:Jimbo99999
ID: 38820364
Hello Everyone

I talked with the author of the code and would be best to leave the logic how it is in the routine.

I have converted the For Each loop to a Do while loop and the error is gone.  However, I am unsure of my structure as I have never used a Do While loop with a DataTable.

Does this look ok:

 Dim rowCount As Integer = 0
 Dim cmdSqlRSDataRow As DataRow
 Do While rowCount <= sqlRStbl.Rows.Count
                cmdSqlRSDataRow = sqlRStbl.Rows(rowCount)

                .....

rowCount = rowCount + 1
Loop

I am unable to test the code as there are still other conversion errors.  So, short of making a little test App I don't know if this code will work.

Thanks,
jimbo99999
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 166 total points
ID: 38820729
Code looks ok except

Do While rowCount <= sqlRStbl.Rows.Count

which should be

Do While rowCount < sqlRStbl.Rows.Count
0
 

Author Comment

by:Jimbo99999
ID: 38826700
Ok, gotcha since the DataTable starts at 0.

Thanks,
jimbo99999
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question