hometown_location is an array – how to access this?

Sep 18, 2009 at 12:23 AM
Edited Sep 18, 2009 at 12:25 AM

The Facebook Developer Toolkit works great, and my FB Desktop application connects to the user data table and I can pull the information I need.  As I have beginner to intermediate skill, I don’t know how to access the few array column types – they come up as null.

I can access all of the int and string data, no problem, but the arrays, well, I’m lost.

Thanks for any assistance with this!

Sep 18, 2009 at 12:45 AM

Which version are you using 2.1 or the 3.0 branch? Also, which properties specifically are you trying to access?  Can you post your code? 

Sep 18, 2009 at 2:37 AM

Thanks for the response!  I'm using 2.0.  My problem is that I don't have the experience in working with arrays, so I don't know how to handle them.  For example, I can access name (string) and uid (int) from the Facebook user table, but I don't know how to access the hometown_location (array) - hometown_location returns null.  Null creates an unhandled exception, and I believe this is because Facebook stores this information as an array, and I believe arrays must be accessed differently than string and int.  The code works fine, except when I try to access any of the array columns from the Facebook user table e.g. affiliations, hometown_location, current_location, hs_info, education_history, work_history, or email_hashes.

Here's some of my code.  The desktop application is supposed to get the uid, name, and hometown_location of the Facebook member using my application.

using facebook.desktop;
using facebook.Linq;
using facebook;
using Microsoft.Xml.Schema.Linq;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System;

namespace MyApplication
{
    public partial class Form1 : Form
    {
         private string strUID;
	private string strName;
	private string strHTown;
        
        //The Application and Secret Keys to login MyApplication to Facebook
        private static string APPLICATION_KEY = "######";
        private static string SECRET_KEY = "######";

        //String for connection to Facebook database (db)
        public static FacebookDataContext db = new FacebookDataContext(
            new API() { ApplicationKey = APPLICATION_KEY, Secret = SECRET_KEY });

        public Form1()
        {
            InitializeComponent();
            db.Api.ConnectToFacebook();

            var user = db.user.Where(u => u.uid == db.Api.uid).First();
            var name = user.name;
            var uid = user.uid;
            var htown = user.hometown_location;

            strName = name.ToString();
            strUID = uid.ToString();
            strHTown = htown.ToString();
        }

      private void Form1_Load(object sender, EventArgs e)
        {
            MyAppData.myappdata_user_infoRow newUserInfo;
            newUserInfo = MyAppData.myappdata_user_info.myappdata_user_infoRow();
            newUserInfo.user_uid = strUID;
            newUserInfo.user_name = strName;
            newUserInfo.user_htown = strHTown;            
            this.MyAppData.myappdata_user_info.Rows.Add(newUserInfo);
            this.taUserInfo.Update(this.MyAppData.myappdata_user_info);
        }

    }
}
When I debug, the application throws this error: System.NullReferenceException was unhandled  "Object reference not set to an instance of an object."
After doing some initial ReGooging I've determined this error is due to my lack of understanding on how to handle arrays, and how to insert them into the MS SQL database.
Thanks again for your assistance!
Sep 18, 2009 at 3:57 AM

I'm not sure if I understand what is going on. I didn't work on any of the 2.x toolkits. I also don't know what FacebookDataContext is. Is that your own linq to sql data context? Here is what i'd suggest so you can get going. Just download the latest release not the 2.1 release, but the latest check-in. The dev branch is the 3.0 version. There is a winform sample in there. Try that out. I think it might be easier to understand if you run that code.  On line 26 in the FriendViewer.cs class in \DEV\Samples\Winforms there is this code:

var me = facebookService1.Users.GetInfo();

to get the city for the home_town location you'd do write "var city = me.hometown_location.city"

hometown_locaiton is an object not an array -  there are a bunch of properties on it.  

In this example facebookService1 is a component you drag on the winform to make it easier. You could also do something like the following if you didn't want to drag that component onto your form. 

 

                var session = new DesktopSession(FB_API_KEY, null, null, true, new List<Enums.ExtendedPermissions>() { 

                    Enums.ExtendedPermissions.read_mailbox, 

                    Enums.ExtendedPermissions.publish_stream, 

                    Enums.ExtendedPermissions.read_stream,

                    Enums.ExtendedPermissions.offline_access});

                session.Login();

            var api = new Facebook.Rest.Api(session);

            var currentUser = api.Users.GetLoggedInUser();

                _session = new DesktopSession(FB_API_KEY, null, null, true, new List<Enums.ExtendedPermissions>() { 
                    Enums.ExtendedPermissions.read_mailbox, 
                    Enums.ExtendedPermissions.publish_stream, 
                    Enums.ExtendedPermissions.read_stream,
                    Enums.ExtendedPermissions.offline_access});

                _session.Login();

 

There is sample code in DEV\Samples\WPF that does pretty much the same thing as i just pasted.  

Does that help?  To fix your current code, I think I'd need to see your whole project because I'm not sure what some of that code is doing like

public static FacebookDataContext db = new FacebookDataContext(
            new API() { ApplicationKey = APPLICATION_KEY, Secret = SECRET_KEY });
Sep 21, 2009 at 3:50 PM

Thank you for the assistance.  Your advice and example was helpful.  Yes, I created my own data context via facebook.linq as I started with a version previous to 2.1, and it was the best way for me to access the FB data tables.  This data context sorta works, but does not allow me to access all of the data columns.

One last question, what is the proper syntax to access the data for the following:

meeting_sex
meeting_for
education_history
work_history

I've played around these, but could not access the data.  I tried to used the syntax you provided for home_town location - but to no avail.

Thank you in advance for your assistance.

 

Sep 21, 2009 at 7:35 PM

Most of those are lists so for meeting_for you could do

 

 

                var info = _api.Users.GetInfo();
                if (info.meeting_for.seeking != null)
                {
                    foreach (string m in info.meeting_for.seeking)
                    {
                        Console.WriteLine(m);
                    }
                }

                var info = _api.Users.GetInfo();

                if (info.meeting_for.seeking != null)

                {

                    foreach (string m in info.meeting_for.seeking)

                    {

                        Console.WriteLine(m);

                    }

                }

 

or for work_info

 

                var info = _api.Users.GetInfo();

                if (info.work_history.work_info != null)

                {

                    foreach (work_info wi in info.work_history.work_info)

                    {

                        Console.WriteLine(wi.company_name);

                    }

                }

 

Oct 7, 2009 at 11:16 PM
Edited Oct 8, 2009 at 1:36 AM

 

Thank you very much for this assistance!  This was a great help.

On a somewhat related note, does Facebook change the data types in columns?  I am receiving an unhandled FormatException for significant_other_id for users that do not have a significant other id in their profile: “Input string was not in a correct format.”

Funny, I don’t recall this exception being thrown for my friends that do not have this data in their profile.  I wish now that I had been a bit more methodical in recording profile data for those users that are helping me with my application test!

Thanks again for your assistance!

Edit:  I tried to use the !=null approach, as you've shown above, to the significant_other_id column, but it does not work.  Thanks to you, it worked great on hometown_location.city, hometown_location.state, and hometown_location.country. If a profile does not contain a significant_other_id, what is in this column if it is not null?

if (city != null)
{
strCity = city.ToString();
}
else
{
strCity = "";
}