Solved

Help filling DataRow objct using TableAdapter

Posted on 2007-04-08
6
755 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

627 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