Solved

VB.Net - Conversion from VB6 with GoTo

Posted on 2013-01-23
14
291 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
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 166 total points
Comment Utility
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
Comment Utility
Can you show some code? Is that loop just making the program wait till the count is greater than 0?
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 84 total points
Comment Utility
I agree.  Probably should change to a While type loop.
0
 

Author Comment

by:Jimbo99999
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Expert Comment

by:contactnaeem
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Code looks ok except

Do While rowCount <= sqlRStbl.Rows.Count

which should be

Do While rowCount < sqlRStbl.Rows.Count
0
 

Author Comment

by:Jimbo99999
Comment Utility
Ok, gotcha since the DataTable starts at 0.

Thanks,
jimbo99999
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now