Solved

Retrieve data from a dataset when one row is returned

Posted on 2011-09-14
12
301 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
[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
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

705 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