Solved

Retrieve data from a dataset when one row is returned

Posted on 2011-09-14
12
296 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 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