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();
            }
        }
  • Can you provide the event code that is failing. – Kris Jan 13 '16 at 09:45
  • It is not failing really there are no exceptions being thrown.. but I will update now @kris –  Jan 13 '16 at 09:47
  • Just because there is no exception doesn't mean it isn't failing. An answer was provided instantly after updating the question. :) – Kris Jan 14 '16 at 02:47

1 Answers1

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();
    }
}
Community
  • 1
  • 1
  • It still returns a zero value in my Service in: `var t = new Feed { Id = feedview.Id };` –  Jan 13 '16 at 10:14
  • `feedview.Id` is 0 in `DelFeed`? And what is the value of `Id` when creating `FeedView`? – Patryk Spytkowski Jan 13 '16 at 10:20
  • 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` –  Jan 13 '16 at 10:22
  • 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`. – Patryk Spytkowski Jan 13 '16 at 10:24
  • It is Also `0` in `((ClientItems)SelectedItem).Id` –  Jan 13 '16 at 10:27
  • You may also set `lbFeed.SelectedValuePath = "FId";` in `LoadFeeds` and try to set `Id = (int)lbFeed.SelectedValue` in `bnFeedDel_Click` – Patryk Spytkowski Jan 13 '16 at 10:27
  • Yeah it shows the value now! seems that my Delete method doesn't work one issue down atleast thanks! –  Jan 13 '16 at 10:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100570/discussion-between-patryk-spytkowski-and-rgdent). – Patryk Spytkowski Jan 13 '16 at 10:39