Solved

Best way to retrieve/process a single row in datatable.

Posted on 2011-03-25
4
457 Views
Last Modified: 2012-05-11
What is the most efficient way to retrieve and process a datatable with a single row?
What I have been doing thus far (because I don't know any other way) is something like the following.

Dim dr As DataRow
For Each dr In dt.Rows
        Return dr("CustID")
Next

Seems like I should be able to reference the contents of the single row with only one statement (i.e. dt.rows(0)<something>

0
Comment
Question by:mdreed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 5

Accepted Solution

by:
puffdaddy411 earned 500 total points
ID: 35218386
If you know the index of the row, the following will return a Row object.

dt.Rows(0)


By adding ".ItemArray" you get an array of the items in that row.

dt.Rows(0).ItemArray


If you know your column names, you can reference those columns OR you can use index numbers.

dt.Rows(0).Item("ColumnNameORIndex")
0
 
LVL 32

Expert Comment

by:Erick37
ID: 35218403
You could use the DataRow's ItemArray property to get or set the values in a row in one shot.
0
 
LVL 10

Expert Comment

by:kdwood
ID: 35218438

If you just want to get the value, how about this:

Dim myCustID as Integer = dt.rows(0).item("CustID")

This would get the CustID value on row 0, the first row returned.

Regards,

Keith

0
 

Author Closing Comment

by:mdreed
ID: 35218469
I knew there should be an easier way !

Sorry, Erick.  puff beat you to it by 2 minutes.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question