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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

É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
santhimurthydCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.