Solved

Help filling DataRow objct using TableAdapter

Posted on 2007-04-08
6
729 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

22 Experts available now in Live!

Get 1:1 Help Now