Solved

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

Posted on 2011-03-25
4
458 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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