Question

I want to get the name of the product which i've selected in my gridview. I have a index number which I can use to compare in my database, but I can't select the name of the item which belongs to that index number.

ServiceReference1.ProductContext ctx = new ServiceReference1.ProductContext(new Uri("http://SERVER:5000/WcfDataService1.svc/"));

DbList = ctx.Products;
int index = ProductsList.SelectedIndex;

string name = DbList.XXXXXXX  // -> ?????????

Everything what i've tried ended up in a exception. Any ideas how to get the product name?

Thanks in Advance.

Was it helpful?

Solution 3

simon at rcl and LaMMMy helped me in the right direction. Thank you!

So my solution for the problem is:

ServiceReference1.Product product = (ServiceReference1.Product)this.ProductsList.Items[productIndex];
string name = product.Name;

OTHER TIPS

Something like (this is air code so might not be right):

Product product = DBList.Where<Product>((p) => p.Id == index);

This assumes that DBList is a list of type product, and that a Product has an Id property which matches the index you're looking for.

You can then do

string name = product.Name;

etc etc etc.

Could you do

Product myProduct = ctx.Products.Find(index);
string name = myProduct.Name;

I'm not sure if this method applied in the DataService Context or not.

Edit: I'm thinking Entity Framework/ApplicationDBContext. I think simon at rcl has the answer with linq.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top