Lolas Laris
Lolas Laris

Reputation: 35

System.InvalidCastException with string isolatedstorage

I get this exception on the following code. An exception of type 'System.InvalidCastException' occurred in Project.DLL but was not handled in user code.

if (IsolatedStorageSettings.ApplicationSettings.Contains("0"))
{
    string temp = (string)IsolatedStorageSettings.ApplicationSettings["0"];//exception here
}

What am I doing wrong?

Upvotes: 1

Views: 472

Answers (2)

user2975038
user2975038

Reputation: 537

I had the same problem! Here is what I did:

if (IsolatedStorageSettings.ApplicationSettings.Contains("0"))
{
   string temp = IsolatedStorageSettings.ApplicationSettings["0"].ToString();
}

Upvotes: 1

Karthik Nishanth
Karthik Nishanth

Reputation: 2020

An InvalidCastException exception is thrown when the conversion of an instance of one type to another type is not supported. It differs from an OverflowException exception, which is thrown when a conversion of one type to another is supported, but the value of the source type is outside the range of the target type.

What type of data are you storing at key "0"? You should be typecasting the Value of key 0 to string before saving it, corresponding to the Dictionary data type?

Upvotes: 1

Related Questions