{"id":1547,"date":"2022-08-30T15:17:26","date_gmt":"2022-08-30T15:17:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/invalid-castexception-was-unhandled-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:26","modified_gmt":"2022-08-30T15:17:26","slug":"invalid-castexception-was-unhandled-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/invalid-castexception-was-unhandled-collection-of-common-programming-errors\/","title":{"rendered":"Invalid CastException Was Unhandled-Collection of common programming errors"},"content":{"rendered":"<p>I am getting the following error when trying to run code from http:\/\/facebooksdk.blogspot.co.uk\/2011\/04\/facebook-groups.html<\/p>\n<p>The error is &#8220;Invalid CastException Was Unhandled&#8221; and &#8220;Specified Cast Is Not Valid&#8221;<\/p>\n<p>I am using the Facebook C# SDK and I am using the following code&#8230;<\/p>\n<pre><code>    public Dictionary GetGroups()\n    {\n        string accessToken = _accessToken;\n        FacebookClient facebookClient = new FacebookClient(accessToken);\n        JsonObject resul = facebookClient.Get(\"https:\/\/graph.facebook.com\/me\/groups?access_token=\" + accessToken) as JsonObject;\n        Dictionary dicGroups = new Dictionary();\n        foreach (JsonObject item in (((KeyValuePair)(resul[0])).Value as JsonArray))\n        {\n            comboBox1.Items.Add(item[\"id\"].ToString());\n        }\n        return dicGroups;\n    }\n<\/code><\/pre>\n<p>Could anyone help? I am trying to get a list of all the users groups and add them to a combobox. Thanks<\/p>\n<ol>\n<li>\n<p>I looked into the Facebook C# SDK more, and the api returns a json string of groups. You need to loop through each object in the json string to get what you want.<\/p>\n<pre><code>JsonObject groups = facebookClient.Get(\"me\/groups\") as JsonObject;\nforeach(var group in (JsonArray)groups[\"data\"])\n{\n    comboBox1.Items.Add(item[\"id\"].ToString());\n    ...\n}\n<\/code><\/pre>\n<p>Note: You need to prompt the user for the <code>user_groups<\/code> extended permission to be able to retrieve groups. Follow this link for an example of prompting for extended permissions. I don&#8217;t have a Facebook app, so I can&#8217;t test prompting for the permissions and retrieving group information.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 05:08:38. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am getting the following error when trying to run code from http:\/\/facebooksdk.blogspot.co.uk\/2011\/04\/facebook-groups.html The error is &#8220;Invalid CastException Was Unhandled&#8221; and &#8220;Specified Cast Is Not Valid&#8221; I am using the Facebook C# SDK and I am using the following code&#8230; public Dictionary GetGroups() { string accessToken = _accessToken; FacebookClient facebookClient = new FacebookClient(accessToken); JsonObject resul [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1547","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1547"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1547\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}