Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help filling DataRow objct using TableAdapter

Posted on 2007-04-08
6
Medium Priority
?
758 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
Independent Software Vendors: 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

670 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