Loop not working

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
LVL 7
ayha1999Asked:
Who is Participating?
 
santhimurthydConnect With a Mentor Commented:
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
0
 
käµfm³d 👽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

0
 
ayha1999Author 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
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
santhimurthydCommented:
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()

0
 
Éric MoreauSenior .Net ConsultantCommented:
I think santhimurthyd nailed it.

I doubt you have access to rows(i) and cells (i) unless you have a perfectly squared matrix
0
 
käµfm³d 👽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

0
 
ayha1999Author 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
0
 
Éric MoreauSenior .Net ConsultantCommented:
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?
0
 
ayha1999Author 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
0
 
ayha1999Author Commented:
Thanks for the help and all of your participation.
0
 
santhimurthydCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.