Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to loop through the rows of a dataset and compare the values of a particular column .
i am using the following code but it does not loop through all the rows in the dataset

VB
For i As Integer = 0 To ds.Tables("Students").Rows.Count - 1
            Dim student_fullname As String
            Dim student_fullname2 As String

            student_fullname = ds.Tables(0).Rows(i)(1).ToString()
            MsgBox("student_name: " + student_fullname)

            For j As Integer = 0 To ds.Tables("Students").Rows.Count - 1
                student_fullname2 = ds.Tables(0).Rows(j + 1)(1).ToString()
                If String.Equals(student_fullname, student_fullname2) Then
                    MsgBox("defaulter: " + student_fullname)
                End If
            Next
 Next
Posted
Comments
DamithSL 13-Dec-14 11:31am    
can you explain in words what exactly you trying to do?
hlsc1983 13-Dec-14 11:38am    
i have a table in sql server with a number of rows. i want to identify those students whose names are present more than once in the database table
DamithSL 13-Dec-14 12:30pm    
I have added solution which using LINQ to find duplicates in datatable.
PIEBALDconsult 13-Dec-14 11:54am    
DataSets don't have rows.
That is a task best performed by the database, not client code.
Are you using SQL Server?
hlsc1983 13-Dec-14 12:05pm    
yes SQL server

That should be done on the server side, not in client code.
This is a stored procedure I use to do that (usually in SSMS, but you should be able to call it from code).

SQL Server Procedure to Report Duplicates[^]
 
Share this answer
 
Comments
Maciej Los 14-Dec-14 10:57am    
+5!
Quote:
i want to identify those students whose names are present more than once in the database table

try below Linq solution
VB
Dim duplicates As List(Of String) = ds.Tables("Students").AsEnumerable().[Select](Function(dr) dr.Field(Of String)("StudentName")).GroupBy(Function(x) x).Where(Function(g) g.Count() > 1).[Select](Function(g) g.Key).ToList()
 
Share this answer
 
v2
Comments
Maciej Los 14-Dec-14 10:57am    
+5!
Well, no - it won't.
You are using the "Students" table to select the number of rows to iterate through, but the first (zeroth) table to compare values against. So if the "Students" table isn't the first one, you will check the wrong number of rows. And if it is, then you will always find the student almost immediately.

And you are exceeding the number of possible rows on the first check: you use 0 to (count - 1) as the loop values, and then add 1 to the row number in the loop - so you check values 1 to count. And since there are only count values you exceed the rows available.
Assume there are three rows, and count is 3. So j will be 0, 1, 2 So you will skip the zeroth row, then access row one, row two, and try to access row three which doesn't exist.

I think you need to sit down, look at your data and work out exactly what you are trying to do: because I don't think that is it, whatever it is you are trying to do!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900