TwitterQueryException was unhandled by user code. Error while querying Twitter

Jun 14, 2012 at 3:28 PM


I am trying to retrieve the Direct Messages to my twitter account, but most of the time getting errors "TwitterQueryException was unhandled by user code. Error while querying Twitter". Here is my code.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
using System.Net;
using LinqToTwitter;

namespace SDSTVWeb
    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            StaticHelper.Auth = new SingleUserAuthorizer
                Credentials = new InMemoryCredentials
                    ConsumerKey = ConfigurationManager.AppSettings["Consumerkey"],
                    ConsumerSecret = ConfigurationManager.AppSettings["Consumersecret"],
                    OAuthToken = ConfigurationManager.AppSettings["twitterOAuthToken"],
                    AccessToken = ConfigurationManager.AppSettings["twitterAccessToken"]
            StaticHelper.TwitterCtx = new TwitterContext(StaticHelper.Auth);
            //Load up the latest direct messages to SDSTV
            var directMessages = (from tweet in StaticHelper.TwitterCtx.DirectMessage
                where tweet.Type == DirectMessageType.SentTo
                select tweet).ToList();  

            rptIncomingMessages.DataSource = directMessages;

Here is the detailed error message on the browser upon build, including stack trace:
Server Error in '/' Application.
The remote server returned an error: (400) Bad Request.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request.

Source Error:

Line 35:           
Line 36:             //Load up the latest direct messages to SDSTV
Line 37:             var directMessages = (from tweet in StaticHelper.TwitterCtx.DirectMessage
Line 38:                 where tweet.Type == DirectMessageType.SentTo
Line 39:                 select tweet).ToList();  

Source File: C:\MAIN\TracerUI\UINET\SDSTVWeb\SDSTVWeb\Default.aspx.cs    Line: 37

Stack Trace:

[WebException: The remote server returned an error: (400) Bad Request.]
   LinqToTwitter.TwitterExecute.QueryTwitter(Request request, IRequestProcessor`1 reqProc) +493

[TwitterQueryException: Error while querying Twitter.]
   LinqToTwitter.TwitterQueryProvider.Execute(Expression expression) +405
   LinqToTwitter.TwitterQueryable`1.GetEnumerator() +53
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +315
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
   SDSTVWeb._Default.Page_Load(Object sender, EventArgs e) in C:\MAIN\TracerUI\UINET\SDSTVWeb\SDSTVWeb\Default.aspx.cs:37
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Thank you.

Jun 14, 2012 at 3:47 PM

Hi Sushan,

TwitterQueryException has a property named Response, of type TwitterHashResponse.  What are the values of Response?


Jun 14, 2012 at 4:10 PM

Hi Joe,


Excuse my lack of knowledge. Where can I find the values of Response, in this case? 

Jun 14, 2012 at 4:29 PM

You'll want to look at the instance of the TwitterQueryException that's thrown.  It has a property named Response.  Here's a couple ideas:

1. Use a try/catch to handle TwitterQueryException and log the details, including the values in the Response property.

2. If the exception is unhandled, VS will break and show an Exception message.  Click on the details link and drill down into the exception object info.