Click here to Skip to main content
14,880,638 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, This is the code written in c# in windows application.
Here I am getting an error in line 6 is:Cannot implicitly convert type 'string' to 'int'

void wc_DownloadStringCompleted1(object sender, DownloadStringCompletedEventArgs e1)
       {
           var serializer1 = new JavaScriptSerializer();
           serializer1.RegisterConverters(new[] { new DynamicJsonConverter() });
           dynamic obj = serializer1.Deserialize(e1.Result, typeof(object));
           dynamic dd =Int32.Parse(obj["HierarchyPosition"]);
           dynamic fn1 =Int32.Parse(obj["Formatted Name"]);
           lblitemname.Text = fn1;
       }



So, how to solve this problem.

Thanks.
Posted
Updated 26-Mar-14 0:19am
v2
Comments
Raul Iloc 29-Mar-14 12:51pm
   
Did you try my solution (Solution 5)?

I'd suspect that the problem is with the indexing by string instead of by int.
I.e., it doesn't like
C#
obj["HierarchyPosition"]

The compile-time error: Cannot implicitly convert type 'string' to 'int'
means you're passing a string where an int is expected.
   
The error is generated because the key for getting data from your deserialized object is expected to be int and not string. So the solution could be :

C#
void wc_DownloadStringCompleted1(object sender, DownloadStringCompletedEventArgs e1)
        {
            var serializer1 = new JavaScriptSerializer();
            serializer1.RegisterConverters(new[] { new DynamicJsonConverter() });
            dynamic obj = serializer1.Deserialize(e1.Result, typeof(object)); //You have to inspect the value of obj after this line in Debug!
            int dd =Int32.Parse(obj[0]); //Or other numeric value!
            int fn1 =Int32.Parse(obj[1]);//Or other numeric value!
            lblitemname.Text = fn1.ToString(); //A string is expected!
        }


You should try the above code, but better is to inspect in Debug the obj object to see its properties and keys.
   
the error clearly shows message that you can not convert string to int format.
you trying the same in 6th line.

try to change:-
dynamic fn1 =Int32.Parse(obj["Formatted Name"]);


to

dynamic fn1 =obj["Formatted Name"].ToString();
   
Comments
Priyanka Bhawsar 26-Mar-14 7:06am
   
Its not accepting when we write .Tostring();
norbitrial 26-Mar-14 7:21am
   
Try .ToString() instead of .Tostring()
Priyanka Bhawsar 26-Mar-14 8:09am
   
sorry I write the same .ToString(); but showing error.
TrushnaK 26-Mar-14 7:29am
   
try with direct object value
dynamic fn1 =obj["Formatted Name"];
Int32.Parse can't convert the value of the obj["Formatted Name"] to int, probably there isn't a correct int format value in it.

If you would like to add as a text the value for your label, try this:
C#
if (obj["Formatted Name"] != null)
{
    lblitemname.Text = obj["Formatted Name"].ToString();
}
   
v4
Comments
Priyanka Bhawsar 26-Mar-14 7:08am
   
but it showing error in this line
dynamic fn1 =obj["Formatted Name"];
norbitrial 26-Mar-14 7:21am
   
Remove that line and use what I've added to my solution.
Priyanka Bhawsar 26-Mar-14 7:27am
   
yes, I tried this. It showing same error.
Try this:

C#
lblitemname.Text = fn1.ToString();
   
Comments
Priyanka Bhawsar 26-Mar-14 7:07am
   
Yes, I did the same thing but it showing same error.
Can you add null check before type casting. It might have null value.
   
This is the solution of my problem.
C#
void wc_DownloadStringCompleted1(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                var serializer1 = new JavaScriptSerializer();
                serializer1.RegisterConverters(new[] { new DynamicJsonConverter() });

                dynamic obj = serializer1.Deserialize(e.Result, typeof(object));
                     
			dynamic obj7 = obj[7]; 
                        dynamic fn7 = obj7["Formatted Name"]; 
                        lblitemname.Text = fn7;
           }
            catch (Exception ex)
            {
            }
        }
   
v2
You can rather use int.TryParse so that id would confirm first that the objects like obj["HierarchyPosition"] is integer value or not
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900