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 8335891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:48:46+00:00 2026-06-09T03:48:46+00:00

I am having a problem with the following code. It uses the Facebook c#

  • 0

I am having a problem with the following code. It uses the Facebook c# SDK. The problem is that I am pulling and printing a list of all of my friends work history. However, when this data isn’t available I think it is causing a NullReferenceException. I have looked at a bunch of solutions online, but haven’t found one that seems to work. Any help would be appreciated.

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var accessToken = "...";
        var client = new FacebookClient(accessToken);
        dynamic myInfo = client.Get("me/friends", new { fields = "name,id,work" });

        foreach (dynamic friend in myInfo)
        {
            foreach (dynamic work in myInfo.work) // <---- here
            {
                Response.Write("Employer: " + work.employer.name + "<br/> Position:" + work.position.name + "<br/><br/>");
            }
        }
    }
}

I have included the suggested edits but am now getting a different error:

    namespace WebApplication1.Site
 {
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        var accessToken = "xxx";
        var client = new FacebookClient(accessToken);
        dynamic myInfo = client.Get("me/friends", new { fields = "name,id,work" });

        foreach (dynamic friend in myInfo)
        {
            **foreach (dynamic work in friend.work ?? new[] { new { employer = new { name = string.Empty }, position = new { name = string.Empty } } })**
                {
                    Response.Write("Employer: " + myInfo.work.employer.name);
                }

        }


    }
}
}

‘System.Collections.Generic.KeyValuePair’ does not contain a definition for ‘work’ on Line 21

  • 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-06-09T03:48:47+00:00Added an answer on June 9, 2026 at 3:48 am

    You can provide an empty list to inplace of the null value to avoid the null reference exception:

     foreach (dynamic work in friend.work ?? new List<string>())
    

    You are getting the exception because foreach makes a call to .GetEnumerator() on a null value. I used a new List, but it doesn’t really matter what is put here.. as long as it isn’t null and it is enumerable.

    Edit

    You could provide an anonymous type that provides the properties you need:

     foreach (dynamic work in friend.work ?? new [] { new { employer = new { name = string.Empty}, position = new { name = string.Empty }}})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with the following code foreach ($ex in (foo, bar, baz))
I'm having a problem with the following code snippet: <!DOCTYPE html> <html> <head> <title>Hangman</title>
The following code is having some problem with the jQuery. <script type="text/javascript"> $(window).load(function() {
I'm having a problem getting a change event to register with the following code:
I'm having a problem with TRY...CATCH blocks. Can someone explain why the following code
I'm having a problem with image scaling. When I use the following code to
I'm having problems the following code gives me no results. however if I uncomment
I'm having problems with the following code (I am a beginner in most things
I'm having following problem. I should convert a control to a certain type, this
I am having the following problem: a) I have a UNIX build environment set

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.