"String was not recognized as a valid DateTime."

Apr 5, 2012 at 6:28 PM

Hi all!

Suddenly I am getting the exception "String was not recognized as a valid DateTime.".

I can't use facesharp at all.     I was told that nothing was changed on our server.

This line wont work:

String fbname = fbApi.User.GetCurrentUser().Id.ToString();

and this line wont work:

String fbname = fbApi.User.GetCurrentUserId().ToString();

I even tried getting Firstname or other string values, but I always get the exception "String was not recognized as a valid DateTime.".

I am asking for a facebook id and I am getting a Date Error !!!!!!!!!

What is the problem?  Please HELP !  

Apr 5, 2012 at 7:12 PM

Can you please post some code so I can assist with this?

Apr 5, 2012 at 7:44 PM
Edited Apr 5, 2012 at 7:46 PM

Hi simnova!
Tell me if you want me to show you other parts of the code.

 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FaceSharp;
using FaceSharp.Api;
using FaceSharp.Api.Types;
using FaceSharp.MVC.Filters;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using myapplication.Code.Facebook;
...
...

if (Page.IsPostBack == false)
{
	FacebookCore fbCore = new FacebookCore();
	FacebookGraphApiFilter fbGraphApiFilter = new FacebookGraphApiFilter();
	SignedRequest fbSignedRequest = fbGraphApiFilter.GetSignedRequest(Request.Form["signed_request"], fbCore.ApplicationSecret);

	if ((fbSignedRequest != null) && (fbSignedRequest.Page != null) && (fbSignedRequest.Page.Liked == true))
	{
		txtdebug.Text = "liked\n\n";
		if (fbSignedRequest.OAuthToken != null)
		{
			txtdebug.Text = "installed\n\n";
			FacebookApi fbApi = new FacebookApi(fbSignedRequest.OAuthToken);
			String strUserFbId = fbApi.User.GetCurrentUser().Id.ToString();
		}
		else
		{
			txtdebug.Text = "not installed\n\n";
		} 
	}
	else
	{
		txtdebug.Text = "not liked\n\n"; 
	} 

}

Apr 6, 2012 at 6:29 AM

The line that throws the exception "String was not recognized as a valid DateTime",  is   

String strUserFbId = fbApi.User.GetCurrentUser().Id.ToString();

 

Am I totally wrong?  This code works perfectly on a different application, on the same IIS.  

Any Ideas?    (besides learning mvc in 1 day...)

 

Apr 6, 2012 at 9:56 AM

ok, I think I found a workaround. 

Instead of using:

FacebookApi fbApi = new FacebookApi(fbSignedRequest.OAuthToken);
String strUserFbId = fbApi.User.GetCurrentUser().Id.ToString();

I used:

GraphBase fbGraphClient = new GraphBase(fbCore);
JObject jobjUser = fbGraphClient.MakeGraphRequest("me", fields: new[] { "id", "name", "first_name", "last_name", "email", "birthday", "gender", "location", "accounts" });
String strUserId = (String)jobjUser["id"];

...and it worked.

Apr 6, 2012 at 10:26 AM
Edited Apr 6, 2012 at 10:27 AM

 

I found what is causing the exception in Facesharp.Api.Objects.User.

It is the line:

 

user.Birthday = DateTime.Parse((string)jObject["birthday"]);

 

 

No error, If you use this instead:

 

user.Birthday = DateTime.ParseExact((string)jObject["birthday"], "d", System.Globalization.CultureInfo.InvariantCulture);
Apr 6, 2012 at 4:08 PM
Thank you very much!

On Apr 6, 2012, at 5:26 AM, "dimitris71" <notifications@codeplex.com> wrote:

From: dimitris71

I found what is causing the exception in Facesharp.Api.Objects.User.

It is the line:

user.Birthday = DateTime.Parse((string)jObject["birthday"]);

If you use instead:

user.Birthday = DateTime.ParseExact((string)jObject["birthday"], "d", System.Globalization.CultureInfo.InvariantCulture);