Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6344337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:37:02+00:00 2026-05-24T20:37:02+00:00

I curently have the below which displasy a messagebox.show if the results is null.

  • 0

I curently have the below which displasy a messagebox.show if the results is null. But it still gives me an unhandled exception.

How can I stop the exception and just let the user enter a different value?

void Trademe_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            if (e.Error == null)


                MessageBox.Show("Wrong try Again");

            var r = XDocument.Parse(e.Result);

Full code for clarity *****************

namespace TradeMe
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            WebClient Trademe = new WebClient();
            Trademe.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Trademe_DownloadStringCompleted);
            Trademe.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Search/General.xml?search_string=" + TradeSearch.Text));

            progressBar1.IsIndeterminate = true;
            progressBar1.Visibility = Visibility.Visible;


        }



        void Trademe_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;






            var r = XDocument.Parse(e.Result);
            if (e.Result == null)
                MessageBox.Show("Wrong try Again");


            // Declare the namespace
            XNamespace ns = "http://api.trademe.co.nz/v1";
            listBox1.ItemsSource = from TM in r.Root.Descendants(ns + "Listing").Take(50)
                                   select new TradeItem
                                   {
                                       ImageSource = TM.Element(ns + "PictureHref").Value,
                                       Title = TM.Element(ns + "Title").Value,
                                       Region = TM.Element(ns + "Region").Value,
                                       PriceDisplay = TM.Element(ns + "PriceDisplay").Value,
                                       ListingId = TM.Element(ns + "ListingId").Value,
                                   };

           ;

            progressBar1.IsIndeterminate = false;
            progressBar1.Visibility = Visibility.Collapsed;
        }



        public class TradeItem
        {
            public string Region { get; set; }
            public string ListingId { get; set; }
            public string PriceDisplay { get; set; }
            public string Title { get; set; }
            public string ImageSource { get; set; }
        }

        private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/TestPage.xaml", UriKind.Relative));
        }

        private void eventhandler(object sender, SelectionChangedEventArgs e)
        {
            var item = (sender as ListBox).SelectedItem as TradeItem;

            if (item != null)
            {
                string id = item.ListingId.ToString();
                NavigationService.Navigate(new Uri("/TestPage.xaml?ListingId=" + id, UriKind.Relative));
            }


        }


    }
}

Full stack trace **************

   at TradeMe.MainPage.<>c__DisplayClass2.<Trademe_DownloadStringCompleted>b__1(XElement TM)
   at System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()
   at System.Windows.Controls.ItemCollection.EnumerableCollectionView.InitializeSnapshot()
   at System.Windows.Controls.ItemCollection.EnumerableCollectionView..ctor(IEnumerable sourceCollection, ICollectionChangedListener collectionOwner)
   at System.Windows.Controls.ItemCollection.UpdateItemsSourceList(IEnumerable newItemsSource)
   at System.Windows.Controls.ItemsControl.ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value)
   at TradeMe.MainPage.Trademe_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
   at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)
   at System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.Delegate.DynamicInvokeOne(Object[] args)
   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
   at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
   at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
   at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
   at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:37:06+00:00Added an answer on May 24, 2026 at 8:37 pm

    You’re checking whether e.Error is null, not whether e.Result is null… and you’re also not returning after showing the message box.

    (It’s hard to know exactly what’s going on – if you could say what exception you’re getting and under what circumstances, that would be useful…)

    EDIT: Okay, from the stack trace, it looks like it’s in the selection part of the iterator. I strongly suspect that one of these lines is failing:

    ImageSource = TM.Element(ns + "PictureHref").Value,
    Title = TM.Element(ns + "Title").Value,
    Region = TM.Element(ns + "Region").Value,
    PriceDisplay = TM.Element(ns + "PriceDisplay").Value,
    ListingId = TM.Element(ns + "ListingId").Value,
    

    My guess is that one of the elements you’re looking for is missing.

    If you change it to the following code, it won’t throw an exception – but any property which would previously have thrown an exception will be null. That may be what you want, but even if not it would at least potentially help you to find the problem:

    ImageSource = (string) TM.Element(ns + "PictureHref"),
    Title = (string) TM.Element(ns + "Title"),
    Region = (string) TM.Element(ns + "Region"),
    PriceDisplay = (string) TM.Element(ns + "PriceDisplay"),
    ListingId = (string) TM.Element(ns + "ListingId"),
    

    The explicit conversion from XElement to string will return null if the element is null, e.g.

    XElement element = null;
    string value = (string) element;
    // value will be null; no exception is thrown
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the query below which selects a forum board, but the board
I am looking for nested menu which can have different background images , below
I currently have a workable Django admin on the left mockup below but want
I have 3 little questions about eric martin's modal window which I can't seem
I have the following line below, which currently displays like 9/29/2012 <%=Convert.ToDateTime(prs[xDate].ToString()).ToShortDateString() %> I
I currently have an array that looks like the one below but I only
In the image below, the top image is what I currently have on a
Currently I have a borderContainer layout as shown below: My code for this layout
I have a python psp page code is shown below. Currently it only prints
I currently have a table of concentrations, which are linked to a table of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.