?
Solved

Retrieve data from a dataset when one row is returned

Posted on 2011-09-14
12
Medium Priority
?
302 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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