InvalidOperationException was unhandled c#

Anyone here knows about c# Please help me, i can not find my error in here.

if (weather != "Data not found")
        {
            richTextBoxWeatherDetails.Clear();

            XmlSerializer result = new XmlSerializer(typeof(Weather.CurrentWeather));
            var w = (Weather.CurrentWeather)result.Deserialize(new StringReader(weather));

            for (int i = 0; i < w.ItemsElementName.Length; i++)
            {
                richTextBoxWeatherDetails.Text += w.ItemsElementName[i] + ": " +w.Items[i] + "\r\n";
            }
        }
        else
        {
            richTextBoxWeatherDetails.Clear();
            richTextBoxWeatherDetails.Text = "Data Not Found!";
        }

This one is a kind of web service, i wanna check the weather in some city of the countries, it should be show data not found when i choose the city that doesn't have info but it always errors. It works fine when i choose the city that have the info. Can anyone help me?? enter image description here

Upvotes: 1

Views: 393

Answers (1)

Oguz Ozgul
Oguz Ozgul

Reputation: 7187

You are testing weather variable like:

if(weather != "Data not found")

C# String comparisons are case sensitive by default. You need a case insensitive comparison.

Change it to

if(string.Compare(weather, "data not found", System.StringComparison.OrdinalIgnoreCase) != 0)

Upvotes: 3

Related Questions