Tuesday, February 15, 2011

LINQ - Not In

Recently I had to do this query and the follwing showed me how to


var query =

from c in dc.Customers

where !(from o in dc.Orders

select o.CustomerID)

.Contains(c.CustomerID)

select c;


I took this from this link, Marco Russo's blog

Friday, December 24, 2010

Facebook OAuth - more information

Recently, I had to incorporate OAuth for Facebook.

I ran into a few issues.

First and foremost, I had a "Invalid redirect_uri: Given URL is not allowed by the Application configuration" error.

After some trolling on the net, I found that this can be corrected by Url-encoding the redirect_uri parameter.

Using C#, you can fix this by simply, using the Server.UrlEncode function.



Then, I hit a "Error validating verification code." error.

This is fixed by using a Http-GET rather than a Http-POST to retrieve the information.

To do this, simply do the following.
1. Instantiate a URI object and passing the https://graph.facebook.com/oauth/access_token with relevant parameters. See here for more info.
2. Create a HttpWebRequest object.
3. Setting the http web request "method" attribute to GET.
4. Create a HttpWebResponse object.
5. Create a stream reader object and this should return you the access_token values.



To log-in without a user, simply add "&type=client_cred" to 1.

Monday, December 6, 2010

MS Chart Visual Studio 2008 Add-On - Error executing child

I installed MS Chart Add-On for Visual Studio 2008, compiled the project and hit the "Play" button and received a yellow error screen as the below.





To fix this issue, what you have to do is to simply add an additional "verb" to the "httpHandlers" section within the "system.web" section of the ROOT web.config file.

See below for an example

<add verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />


I found this fix at handy Stack-Overflow. Click here

Wednesday, December 1, 2010

Google maps - Overcoming rotating label text for the x-axis

Recently, I had to integrate with Google Charts and ran into the problem of rotating the labels of the x-axis. There isn't a fix for this, however, I manage to do a work around by shortening the labels on the x-axis and creating a legend to explain what the labels meant.

Click here for a sample Google chart.



Thursday, November 18, 2010

Twitter OAuth - How To

Step 1:
Register your application. Don't know if you've to Callback URL field but I just assigned to a HTML page that contain very little information.

Step 2:
Get Twitterizer library from here

Step 3:
Use the following code to get the Twitter OAuth token. I used a session to store it but essentially, as long as you store it either using a Cookie, ViewState, Session, row on the DB, that's totally up to you.


OAuthTokenResponse oatResponse = OAuthUtility.GetRequestToken(CONSUMERKEY, CONSUMERSECRET, HttpContext.Current.Request.Url.AbsoluteUri);

if ((Request.QueryString["oauth_token"] == null) && (Session["tweetOAuthToken"] == null))
{
Uri uri = OAuthUtility.BuildAuthorizationUri(oatResponse.Token);
Response.Redirect(uri.AbsoluteUri);
}
else
{
oatResponse = OAuthUtility.GetAccessToken(CONSUMERKEY, CONSUMERSECRET, Request.QueryString["oauth_token"], Request.QueryString["oauth_verifier"]);

//Add to Session that will expire in 24 hours
OAuthTokens oAuthToken = new OAuthTokens();
oAuthToken.AccessToken = oatResponse.Token;
oAuthToken.AccessTokenSecret = oatResponse.TokenSecret;
oAuthToken.ConsumerKey = CONSUMERKEY;
oAuthToken.ConsumerSecret = CONSUMERSECRET;
Session.Add("tweetOAuthToken", oAuthToken);
Session.Timeout = 1440;

Response.Redirect("TwitterLanding.aspx");
}


NOTES:The consumer token and secret is obtained when you register your application and there is definately a requirement to tidy up this code but it works

Monday, October 25, 2010

Server-side button click - client side implementation

Recently, I had to override the server-side button OnClick function for a button but unfortunately and I hope you do NOT come across this scenario, but the source code file, the C# file went MISSING.

Yes, I could have use DotNet Reflector to recover the code but I was under a very quick deadline, so here is my approach.

Use jQuery!

What you have to do is to assign a unique CssClass name for the button, which can still be done with just the .aspx file.

And include jQuery as per normal the crucial part is when you declare the jQuery document.ready function, include the following within the document.ready function.

That way, you override the ASP.Net page life cycle, thus when you click the button, this code snippet runs first.

$("input.[insert_unique_css_class_here]").click(function() {
//Validate some other controls. You will have to implement the validateChkBox and validateDdl functions.
var postBackRequired = false;
var postBackIfCbChecked = validateChkBox();
var postBackIfDdl = validateDdl();

if (postBackIfCbChecked && postBackIfDdl) {
postBackRequired = true;
}

return postBackRequired;
});

Wednesday, October 6, 2010

Repeater Paging - Paged Data Source

Recently I was experimenting with paging and repeaters and managed to solve it with a PagedDataSource object.

There are certain rule that come with this, however.

After instantiating a PagedDataSource object, it is PIVOTAL to set the DataSource property directly after it, before using other properties of the PagedDataSource class.

The way I implemented it was as follows.

Have 1 repeater that will contain the list of data that would like to display.

Also, I've used a repeater approach to implement the paging numbers rather than having just the 1 page number and having 2 links that will go forward and backward.

I've also maintained the page number using a public property with ViewState holding the value of the current page.