We help IT Professionals succeed at work.

Loop not working

ayha1999
ayha1999 asked
on
I am trying the follwoing code it I don't the correct result.

I am looping an asp.net table and and assigning letters to the values in the table and display.
eg. 1000 100 10 should give abc but the result comes only c.
        Dim result As String = String.Empty

        For i As Integer = 0 To Table1.Rows.Count - 1
            Select Case Trim(Table1.Rows(i).Cells(i).Text)
                Case "10"
                    result = result & "a"
                Case "100"
                    result = result & "b"
                Case "1000"
                    result = result & "c"
                Case Else
                    result = result & String.Empty
            End Select
        Next
        lblResult.Text = result.ToString()

please hlep me to fix the code

thanks

ayha
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2015

Commented:
Do you realize that since you are using i to index both the Rows property and the Cells property that you are basically moving diagonally within your grid? My guess is that you are not indexing the proper cell, and that your Case Else is firing after the first iteration. This appends to empty strings on the subsequent runs.

Perhaps this is what you intended?

Select Case Trim(Table1.Rows(i).Cells(0).Text)

Open in new window

Author

Commented:
I changed it but stilling the getting the same result. I have only one row in the table.

when I change For i As Integer = 0 To Table1.Rows.Count - 1

to For i As Integer = 0 To Table1.Rows.Count

the result is bc otherwise c only
check this

Dim result As String = String.Empty

        For i As Integer = 0 To Table1.Rows.Count - 1
            Select Case Trim(Table1.Rows(i).Cells(0).Text)
                Case "10"
                    result = result & "a"
                Case "100"
                    result = result & "b"
                Case "1000"
                    result = result & "c"
                Case Else
                    result = result & String.Empty
            End Select
        Next
        lblResult.Text = result.ToString()

Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
I think santhimurthyd nailed it.

I doubt you have access to rows(i) and cells (i) unless you have a perfectly squared matrix
Most Valuable Expert 2011
Top Expert 2015

Commented:
I have only one row in the table.
Ah, then you need to swap the indexes.

Select Case Trim(Table1.Rows(0).Cells(i).Text)

Open in new window

Author

Commented:
I tried all methods suggested by you but not worked. the html output of the table as follows

 <table id="Table1" ViewStateMode="Enabled" border="0" style="border-color:Black;border-width:1px;border-style:solid;height: 25px; width: 30px">
      <tr style="border-color:Black;">
            <td id="Cell_0" align="center" style="border-color:Black;border-width:1px;border-style:solid;">20</td><td id="Cell_1" align="center" style="border-color:Black;border-width:1px;border-style:solid;">9</td>
      </tr>
</table>

and the code is

 Dim result As String = String.Empty

        For i As Integer = 0 To Table1.Rows.Count - 1
            Select Case Trim(Table1.Rows(0).Cells(i).Text)  <--- Rows(i).Cells(0) I tried
                Case "20"
                    result = result & "a"
                Case "9"
                    result = result & "b"
                Case "1000"
                    result = result & "c"
                Case Else
                    result = result & String.Empty
            End Select
            i += 1
        Next
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
haven't you said you have just 1 row in the table? there is no need to loop then. Can you provide us a better idea of what is the structure of Table1?
Check this

Dim introwCount As Integer = Table1.Rows.Count
        Dim intColumnCount As Integer = Table1.Rows(0).Cells.Count

        For i As Integer = 0 To introwCount - 1
            For cl As Integer = 0 To intColumnCount - 1
                Select Case Trim(Table1.Rows(i).Cells(cl).Text)
                    Case "20"
                        result = result & "a"
                    Case "9"
                        result = result & "b"
                    Case "1000"
                        result = result & "c"
                    Case Else
                        result = result & String.Empty
                End Select
            Next
        Next

Author

Commented:
@santhimurthyd

your modified code is working fine and get the result as expected. what was wrong with my code? is the new code ok if more that one row exists?

thanks

Author

Commented:
Thanks for the help and all of your participation.
No
In your code you have taken only the rows and not on cells, that's the issue and after going through every row and cell by placing two for loop. Inyour case it was one loop.