Solved

Retrieve data from a dataset when one row is returned

Posted on 2011-09-14
12
294 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now