0

I have one application that has several pages; therefore, I want user to navigate to and back from one page to another with the some data sent back and forth. I could navigate to next page with some specific data by using

NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative));

On next page "Product List.xaml", it can receive data sent to it and do query fine. However, on "Product List.xaml" I put one button and set its event to "NavigationService.GoBack()" so user can press to go to the previous page. At this point, when it is back to the previous page, I got an error at

 private void lstb_prod_cate_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative));
        } 

This is the first page that it will send data to next page

Product Category.xmal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Xml.Linq;
using App_Skin_Test_Final_.All_Files.Database_XML;

namespace App_Skin_Test_Final_.All_Files.Product_Files
{
    public partial class Product_Category : PhoneApplicationPage
    {

        public Product_Category()
        {
            InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            MessageBox.Show("OnNavigateTo");
            base.OnNavigatedTo(e);
            // Do something when the page first loaded
        }


        private void lstb_prod_cate_Loaded(object sender, RoutedEventArgs e)
        {

            XDocument xd = XDocument.Load("All Files/Database XML/ProductsDry.xml");
            var data = from q in xd.Descendants("DryCategory")
                       orderby q.Attribute("DryCategoryName").Value
                       select new ProductsDry
                       {
                           DryCategoryName = q.Attribute("DryCategoryName").Value,
                           DryCategoryId=q.Attribute("DryCategoryId").Value
                       };
            //       MessageBox.Show();
            lstb_prod_cate.DataContext = data;

        }

        private void lstb_prod_cate_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative));
        }

    }
}

And this is a next page "Product List.xaml.cs" that it will receive data and can press button to go back

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Xml.Linq;
using App_Skin_Test_Final_.All_Files.Database_XML;
using System.Windows.Media.Imaging;

namespace App_Skin_Test_Final_.All_Files.Product_Files
{
    public partial class Product_List : PhoneApplicationPage
    {
        string pro_cate_id;
        public Product_List()
        {
            InitializeComponent();

        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.TryGetValue("pro_cate_id", out pro_cate_id))
            {

            }
        }

        private void lst_product_Loaded(object sender, RoutedEventArgs e)
        {

            MessageBox.Show(pro_cate_id);
            XDocument data = XDocument.Load("All Files/Database XML/ProductsDry.xml");
            var productListData = from q in data.Descendants("DryCategory")
                                  from itemDry in q.Elements("ItemDry")  // mean: itemDry in in DryCategory
                                  where q.Attribute("DryCategoryId").Value == pro_cate_id
                                  select new ProductsDry
                                  {
                                      ItemDryName = itemDry.Attribute("ItemDryName").Value,
                                      ItemDryImage=getImage(itemDry.Attribute("ItemDryImage").Value),
                                      ItemDryId=itemDry.Attribute("ItemDryId").Value
                                  };


            lst_product.DataContext = productListData;

         //   NavigationService.GoBack();
        }

        private System.Windows.Media.ImageSource getImage(string p)
        {
            return new BitmapImage(new Uri(p, UriKind.Relative));

        }


        private void lst_product_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/All Files/Product Files/Product Detail.xaml?itemId="+(lst_product.SelectedItem as ProductsDry).ItemDryId,UriKind.Relative));


        }

        private void btnGoBack_Click(object sender, RoutedEventArgs e)
        {


            if (this.NavigationService.CanGoBack)
            {
                this.NavigationService.GoBack();
            }
        }
    }

}

Please, can anyone help me with this error? Thanks

  • 1
    you got an error. And the error is ? You forget to mention that one – Mark Aug 25 '14 at 06:19
  • yellow high light pointed at `NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative)); ` with the pop up window say: ** An exception of type 'System.NullReferenceException' occurred in App_Skin_Test(Final).DLL but was not handled in user code Additional information: Object reference not set to an instance of an object. Check to dertermine if the object is null before calling the method. Use the "new" keyword to create an object instance. Get general help for this exception ** – user2273145 Aug 25 '14 at 07:22
  • Please let me know if you need further more information, thanks – user2273145 Aug 25 '14 at 07:24
  • so the error messages tells you everything :) `(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId` something in there is null, debug it! and maybe you want to look here [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mark Aug 25 '14 at 07:25
  • 1
    just some shots in the blue, there can be a NullReferenceException when there is no SelectedItem and when the SelectedItem Type is not PropductsDry. – Mark Aug 25 '14 at 07:28
  • I also suspected on lstb_prod_caste.SelectedItem is null or the new object of Class ProductsDry is null. and One more, this call is in event on List_box_SelectionChanged, so why when it navigated back, it call this event, if I did not make any change, or selected any item in listbox? So, I think of how to skip this event when page is navigated back, or making an default value for it so it won't be null. Kindly advise, thanks – user2273145 Aug 25 '14 at 07:31

1 Answers1

0

As stated in your comment that you suspected SelectedItem being null. You can avoid exception in that situation by doing a simple check and continue your code only if SelectedItem is not currently null :

private void lstb_prod_cate_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if(lstb_prod_cate.SelectedItem != null)
        NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative));
} 
har07
  • 88,338
  • 12
  • 84
  • 137
  • I followed your instruction, and It works for me by this way `private void lstb_prod_cate_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (lstb_prod_cate.SelectedItem!=null) NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id=" + (lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative)); else lstb_prod_cate.SelectedItem = 1; } ` Thanks very much for your prompt support :) – user2273145 Aug 25 '14 at 10:21
  • ups.. just realize that I didn't initially type the `if` condition correctly. You're welcome anyway – har07 Aug 25 '14 at 10:25