Solved

Retrieve data from a dataset when one row is returned

Posted on 2011-09-14
12
299 Views
Last Modified: 2012-05-12
This may be a silly question but I'll ask anyway.  if you know a dataset is going to contain one row, is there a way to retrieve the data other than using a for next loop?  For example I know there is only going to be one row returned, but I'm coding this as:

For Each trow As DataRow In dsTier.Tables("Tier").Rows
    sTier = trow.Item("Tier")
Next

Just want to make sure I'm programming efficiently.  Thank you.
0
Comment
Question by:kshumway
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 36536806
something like

sTier = dsTier.Tables("Tier").Rows(0).Item("Tier")
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36536810
You can use LINQ to return the First or default.  Do you need code suggestion for LINQ?
0
 
LVL 12

Expert Comment

by:viralypatel
ID: 36536823
you can use this:

dataSet.Tables[“TableNameOrIndex”].Rows[0]["ColumnNameOrIndex"];

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:kshumway
ID: 36536828
Yes  please!
0
 

Author Comment

by:kshumway
ID: 36536841
Thank you, viralypatel.  I'll try that!
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 36536868
And heres the LINQ statement
sTier = (From ds As DataRow In dsTier.Tables("Tier").Rows Select ds.Item("Tier")).FirstOrDefault

Open in new window

0
 
LVL 40
ID: 36537014
And once again, the common mistake. If you have only one table, do not use a DataSet, use a DataTable. A DataSet is a collection of tables with relations. If you do not have relations (and you don't when you have only one table), you do not need a DataSet.

That confusion comes from limitations on the DataTable in Visual Studio 2002. Programmers alwasy used DataSet because of a few commands that were not avaialble in the DataTable, such as SaveXML. This was corrected in 2003. But more than half the code we see everywhere still uses a DataSet when there is only one table or when there are no relations between tables.
0
 

Author Comment

by:kshumway
ID: 36537119
If a datatable is more efficient than using a dataset when I have only one table, I'd like to try that.  But I'm not sure of the syntax to fill a datatable.  Can you help?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36537205
what syntaxdo you have filling the dataset? Anyhow, have you got response to your initial question?
0
 
LVL 40
ID: 36537277
You use exactly the same syntax to fill a DataTable as you do with a DataSet. You will see that the Fill command accepts either one.

Dim tb As New DataTable
'Same stuff for Connection and Command objects
DataAdapter.Fill (tb)

For Each trow As DataRow In tb.Rows
    sTier = trow.Item("Tier")
Next

Open in new window

0
 

Author Closing Comment

by:kshumway
ID: 36537827
Thank you - your code worked perfectly!
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36537856
You are welcome. And I hope you sorted that dataSet - DataTable conundrum too!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Sql server insert 13 36
vb.net 1 month apart 11 35
Optimize the query 5 43
Vb.net threads keep increasing 2 35
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 …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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