Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to loop thru the data table

Status
Not open for further replies.

njahnavi

Technical User
Dec 29, 2008
136
US
Hi I am using vb.net version 2.0

I have a datatable in the dataset.
The data table is "Employee table"
The data table contains "Employeename " field.
I want to loop thru the Employee table like once for each employee since i may have many rows for each employee..

How can i use for each statement for a data table...

I am very new to vb ..

Any help is greately appreciated.
 
I have a table adapter in the dataset not the datatable...
 
To loop through the DataRows of a DataTable, do the following:
Code:
For Each dr As DataRow In YourDatatable.Rows
  MessageBox.Show(dr.Item("SomeColumnName").ToString())
Next
 

If your datatable is created based on the SQL that ends with: "ORDER By Employeename", you may do:
Code:
[blue]Dim strEmplName As String[/blue]
For Each dr As DataRow In YourDatatable.Rows
  [blue]If strEmplName <> dr.Item("Employeename").ToString() Then[/blue]
    MessageBox.Show(dr.Item("Employeename").ToString())
    [blue]strEmplName = dr.Item("Employeename").ToString() 
  End If[/blue]
Next

Have fun.

---- Andy
 
One thing I would add is if have not pulled the DataTable from the DataSet you could do so like:
Code:
Dim CurrentDT As New DataTable = YourDataSet.("TableName")
For Each dr As DataRow In CurrentDT.Rows
or
Code:
For Each dr As DataRow In YourDataSet("TableName").Rows

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
actually i was looking for something which mimics

select distinct employee from emp table

can we do that using vb.net
"for each employee in emp table"...
some thing like that...

I greatly appreciate your support.
 
Why don't you just fill a DataTable from the database with a list of employees?
 
may be i can do that..however our company has some regulations so they just give us acess to the dataset in the vb.net not to the actual database..soem security reasons..

I know it would be quite easy to write a stored procedure..however i was wondering what can i do for the same fucntionality in the vb.
 
I would ask the person in charge to let you have an Employee dataset. I never like having to work around the best process and end up with a less efficient application. However, if you cannot do that, here is one way to get a table of just employees. You might check out LINQ to see if you can do something more efficiently there.

Code:
Dim Employees As DataTable = New DataTable
Employees.TableName = "Employees"
Employees.Columns.Add("EmployeeName", System.Type.GetType("System.String"))

For Each dr As DataRow In YourDataTable.Rows
        If Employees.Select("EmployeeName = '" & dr.Item("EmployeeName") & "'").length = 0 Then
            Dim NewRow As DataRow = Employees.NewRow
            NewRow.Item("EmployeeName") = dr.item("EmployeeName")
            Employees.Rows.Add(NewRow)
        End If
Next

        For Each EmployeeRow As DataRow In Employees.Rows
            'Do something with each employee
        Next
 
Unfortunately, the DataTable.Select method does not accept DISTINCT. I found this method to do this though:


It might be a bit slow if you have a large amount of data; I haven't actually tested it.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top