Solved

Help filling DataRow objct using TableAdapter

Posted on 2007-04-08
6
752 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
[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
  • 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
Industry Leaders: 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!

 
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

Technology Partners: 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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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