Solved

Help filling DataRow objct using TableAdapter

Posted on 2007-04-08
6
736 Views
Last Modified: 2008-01-09
I have a situation where I have a Dataset with a table adapter that queries a table using a parameter that is the tables primary key.

The returned data is always a single row.

I thought it would be more efficient to just fill a DataRow instead of a DataTable..perhaps I am wrong.

Normally when I fill a DataTable using a tableadapter I do something like this

ProductsTabelAdapter adapt = new ProductsTableAdapter();
ProductsDS.ProductsDataTable prodTB = new Products.ProductsTableAdapter();
prodTB = adapt.GetDataBy(prodid);

I thought that filling a DataRow would have been something similar

ProductsTableAdapter adapt = new ProductsTableAdapter();
ProductsDS.DataRow prodrow = new ProductsDS.DataRow();
prodrow = adapt.GetDataBy(prodid);

Apparently I am wrong.

This returns an error

Cannot implicitly convert type ProductsDS.ProductsDataTable to ProductsDS.ProductsRow

Anyone able to point me in the right direction

0
Comment
Question by:Prysson
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:Yttribium
ID: 18873132
try this

try
{
   adapt.GetDataBy(prodid)[0];
}
catch (Exception ee) { //error }

This should return the only row.
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18873142
Miss clicked, I meant to say something like

adapt.GetDataBy(prodid).Rows[0]

Hope this works :)  If not, then it's hard to help without the dependencies.


0
 

Author Comment

by:Prysson
ID: 18873242
Let me try explaining it a different way...

When I fill a DataTable from a TableAdapter I usually do something like this.

ProductsTabelAdapter adapt = new ProductsTableAdapter();
ProductsDS.ProductsDataTable prodTB = new Products.ProductsTableAdapter();
prodTB = adapt.GetDataBy(prodid);

But sometimes the returned value is just a single row.

In the example above that would be the case.

The prodid variable is the primary key of the table being queried so the returned value is only a single row.
 
If the table was

ProdID  Product  Price
1         Product1  1.000

and I set the prodid variable to 1 then obviously the table would only have a single row with the following information.

ProdID  Product  Price
1         Product1  1.000

Now that I have my DataTable returned with this single row..lets say I want to grab the value of the Price and set that to a double variable.

The obvious way is to do a

foreach(DataRow r in prodTB)
{
double price = Convert.ToDouble(r["Price"]);
}

Now that is certainly going to get me where I want to go...

I just cant help thinking there is a more efficient way to deal with this. There is only one row..surely there is a way to grab the value of the "price" column without doing a foreach statement?

Thats really what I am trying to accomplish...If using a DataTable is the best approach thats fine by me..I just am trying to be more efficient with the code and running a foreach loop on a single row strikes me as a little bit odd.


0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 5

Accepted Solution

by:
Yttribium earned 500 total points
ID: 18873308
Well yes it is, and the foreach is not inefficient as it seems, if it only has 1 row, it's doing 1 more check than it would if you got the row directly by index or otherwise.

I don't see what's wrong with your approach to be honest, I think it's perfectly fine, but if you don't want to do for each and KNOW you will only GET one (and will at least get one) you could like I said, reference it directly by index.  You have to fill a table, as far as I know you can't just directly fill a row.  So, the worst that comes out of it, is you have a way of checking that you indeed ARE only getting 1 row :)

 If anyone else has an alternative opinion or contraction, feel free to speak.  This is the way I would have done.

For example, you could just go

instead of:
foreach(DataRow r in prodTB)
{
double price = Convert.ToDouble(r["Price"]);
}

just say

double price = Convert.ToDouble(prodTB[0]["Price"] // not 100% sure about the row indexing here, since I don't have VS infront of me here, but it's something to that effect.  (The intellisense should enlighten you of this possibility).

Good luck!
0
 

Author Comment

by:Prysson
ID: 18873344
Thats what Im looking for.

Thanks for your help with this. Sorry for not being too precise with my opening question.

0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18873391
No problem at all, it's fun to help out  here :)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reset asp.net login password 4 42
Hey Audio! 3 30
Calculate number of nights between two dates 5 25
c# - Best approach for objects in functions 3 16
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 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