0

I Have one table of data:

tblFeed

Id

Title

Content

And I populated a Listbox in my WPF application with this table.

I have the issue now of using the Id value for an event but the Id keeps returning 0.

Any Suggestions?

WCF

public List<Feed> GetFeed()
 {
 List<Feed> r = new List<Feed>();
 List<Feed> e;
 using (TruckDb db = new TruckDb())
 e = db.Feed.Where(x => x.Id != null).ToList();
 foreach (var a in e)
 {
 var feed = new Feed()
 {
 Id = a.Id,
 Title = a.Title,
 Content = a.Content
 };
 r.Add(feed);
 }
 return r;
 }

WPF

 public async Task LoadFeeds()
 {
 TruckServiceClient TSC = new TruckServiceClient();
 try 
 { 
 List<ClientItems> feeditems = new List<ClientItems>();
 if (lbFeed.Items.Count <= 0)
 foreach (var item in await TSC.GetFeedAsync())
 {
 feeditems.Add(new ClientItems
 {
 FId = item.Id,
 FTitle = item.Title,
 FContent = item.Content
 });
 }
 lbFeed.ItemsSource = (feeditems.ToArray());
 lbFeed.DisplayMemberPath = "FTitle";
 }
 catch (Exception)
 {
 throw;
 }
 }
public class ClientItems
 {
 public int FId { get; set; }
 public string FTitle { get; set; }
 public string FContent { get; set; }
 public override string ToString()
 {
 return FTitle;
 }
 }

Delete Event

WCF

 private void bnFeedDel_Click(object sender, RoutedEventArgs e)
 {
 TruckServiceClient service = new TruckServiceClient();
 service.DelFeedAsync(new FeedView
 {
 Id = lbFeed.SelectedIndex
 });
 }

WPF

 public void DelFeed(FeedView feedview)
 {
 using (var result = new TruckDb())
 {
 var t = new Feed
 {
 Id = feedview.Id
 };
 result.Feed.Remove(t);
 result.SaveChanges();
 }
 }
asked Jan 13, 2016 at 9:33
3
  • Can you provide the event code that is failing. Commented Jan 13, 2016 at 9:45
  • It is not failing really there are no exceptions being thrown.. but I will update now @kris Commented Jan 13, 2016 at 9:47
  • Just because there is no exception doesn't mean it isn't failing. An answer was provided instantly after updating the question. :) Commented Jan 14, 2016 at 2:47

1 Answer 1

0

In your bnFeedDel_Click method you are doing this:

Id = lbFeed.SelectedIndex

I think this is your problem as you don't want to set Id to a SelectedIndex value but rather:

[EDIT after some discussion]

Set SelectedValuePath inside LoadFeeds:

lbFeed.SelectedValuePath = "FId";

And use SelectedValue instead of SelectedIndex:

private void bnFeedDel_Click(object sender, RoutedEventArgs e)
{
 TruckServiceClient service = new TruckServiceClient();
 service.DelFeedAsync(new FeedView
 {
 // Of course you may want to check for nulls etc...
 Id = (int)lbFeed.SelectedValue;
 });
 }

Also, you should use DbSet.Attatch() before deleting a record:

public void DelFeed(FeedView feedview)
{
 using (var result = new TruckDb())
 {
 var t = new Feed
 {
 Id = feedview.Id
 };
 result.Feed.Attatch(t);
 result.Feed.Remove(t);
 result.SaveChanges();
 }
}
answered Jan 13, 2016 at 10:04
Sign up to request clarification or add additional context in comments.

8 Comments

It still returns a zero value in my Service in: var t = new Feed { Id = feedview.Id };
feedview.Id is 0 in DelFeed? And what is the value of Id when creating FeedView?
There are Four Id's 1,2,3 and 4 no matter which one I select it when it passes through the delete method it shows 0
Ok, and when you set a breakpoint in bnFeedDel_Click what is the value of ((ClientItems)SelectedItem).Id? I'm trying to understand if the problem is inside DelFeed or somewhere neer the ListBox.
It is Also 0 in ((ClientItems)SelectedItem).Id
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.