Showing posts with label errors. Show all posts
Showing posts with label errors. Show all posts

Tuesday, February 1, 2011

How to sort a YouTube Query with the .NET API SDK

Having trouble sorting the .NET YouTube API video feed?

Countless sites will tell you than in order to sort your videos by the date they were published, all you need to do is set the query.OrderBy property or in some instances the query.setOrderBy property to Google.GData.YouTube.YouTubeQuery.OrderBy.Published.

While working with Intellisense, I noticed that no such values are provided by the OrderBy method. I attempted just using a string query.OrderBy = "Published" but got an error "". Likewise, I thought maybe it's a numerical Enum type and attempted query.OrderBy = 1, but still no luck.

Finally after doing a bit of trial and error, I got it to work with the string "published" (all lowercase).

query.OrderBy = "published"

You can order your videos by a couple different fields, but the string must match these options case sensitvely:

relevance
published
rating
viewCount
Notice: viewCount has an uppercase C

Thursday, January 13, 2011

Given Path Not Supported on Uploads with PostedFile.FileName

Have you intermittently been experiencing issues with uploads on your ASP.NET site?
Seeing errors thrown like "The given path is not supported"?

I recently stumbled across this issue with a couple of pages. I was able to dig down and fix all of my code to handle uploads again.

It looks like the code I was formerly using to save files to my web server is no longer functioning the way it used to. Previously, I saved files something to the style of:
myFileUpload.SaveAs("C:\uploads\" & myFileUpload.PostedFile.FileName)

When I started exploring the error, I discovered that the file path being generated was:
C:\uploads\C:\Users\robfine\Desktop\myFileToUpload.txt

It seems that the function .PostedFile.FileName no longer returns just the name of the file being uploaded, but rather the entire path the file was located at on the uploader's computer.

It's an easy solution to the issue; all you need to do is use the IO.FileInfo class to extract the name of the file:
Dim myFileName as String = New IO.FileInfo(myFileUpload.PostedFile.FileName).Name
myFileUpload.SaveAs("C:\uploads\" & myFileName)


I've done a little of research and it looks like this may be linked to the browser the end user is using. Some version of IE7 and a couple other browsers throw the entire path of the file out there, while other browsers may only give the filename.

Since you never know what browser your user will be using, it's best to cast the .PostedFile.FileName result into the FileInfo class just to be on the safe side.

Thursday, January 6, 2011

Resolving Facebook.Web assembly error in web.config

I've recently been doing a lot of work with the Facebook SDK Toolkit and found myself with an issue when I uploaded a parent site up to my GoDaddy account.

I have a domain with several folders that host other sites, configured as application roots, but when I uploaded my facebook integrated site to the parent domain, each of the folder sites began experiencing server errors despite being marked as an application root: Could not load file or assembly 'Facebook.Web' or one of its dependencies. The system cannot find the file specified.

I did some research and came up bare, but I was able to figure out the issue myself. I track the bug down to a line in my web.config file:
<httpHandlers>
<add verb="*"
path="facebookredirect.axd"
type="Facebook.Web.FacebookAppRedirectHttpHandler, Facebook.Web" />
</httpHandlers>

This line came from the samples provided with the toolkit, but it appears to be unnecessary for my site's needs, so I just took it out. Everything seems to be working fine now.

Monday, January 3, 2011

Resolving Body OnLoad Error BC30456 when runat="Server"

Having trouble loading a javascript function with your body onload call with ASP.NET? If you're getting the Compiler Error Message: BC30456: 'myFunction' is not a member of 'ASP.myMaster', then chances are you've probably set your body to runat="server".


Luckily, there is a way to keep your body running at the server level and a javascript function to the onload method.

First, remove the onload attribute from your body tag:
<body runat="server" id="masterbody">

Now, in the code behind your page or in my case, the master page, add the following code:
masterbody.Attributes.Add("onload", "javascript:myFunction();")

Thursday, July 1, 2010

Easy Way To Email Errors From Your ASP.Net Site To You

Errors on your site are never good, but it's best when you know about them the second they happen rather than a user telling you- or worse- not telling you.

This is a quick a dirty method I use for emailing myself details about errors when they occur on my sites.

If you don't already have one, add a Global Application Class (Global.asax) to your site. It should come with a sub procedure named Application_Error, but if it doesn't go ahead and create one. In the code I've provided below, I use a custom email class I use to templatize my emails, but you can easily adapt it to any email class you choose.

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs

Dim mm As New MMEmail()
mm.ToRecipients.Add("")
mm.Subject = "[ERROR] " & Request.Url.AbsoluteUri.ToString

Dim ex As Exception = Server.GetLastError().GetBaseException()
Dim body As String = ""

'Get URL where error occured
body &= "[URL]"
body &= "<br/>"
body &= Request.Url.AbsoluteUri.ToString
body &= "<br/>"
body &= "<br/>"

'Brief message of what went wrong and line where error occured in code.
body &= "[MESSAGE]"
body &= "<br/>"
body &= ex.Message.Replace(vbCrLf, "<br/>")
body &= "<br/>"
body &= "<br/>"

body &= "[SOURCE]"
body &= "<br/>"
body &= ex.Source.Replace(vbCrLf, "<br/>")
body &= "<br/>"
body &= "<br/>"

body &= "[STACK TRACE]"
body &= "<br/>"
body &= ex.StackTrace.Replace(vbCrLf, "<br/>")
body &= "<br/>"
body &= "<br/>"

mm.Body = body
mm.Send()
End Sub


Obviously, you'll need to add your email address in and use either the default email class or another class if you'd prefer. You'll need the email to be set up in the web.config - which may be another good topic I could touch on another time. And then, it wouldn't hurt to go intentionally create an error. I made a blank page with the on load function that calls a function s() which doesn't exist:
Public Sub Form_Load() Handles Me.Load
s()
End Sub


Monday, June 14, 2010

AJAX and URL Rewrite: Sys is undefined Issue

I created a website on a subfolder of my domain, and later moved it to a subdomain as its own application. Oddly, after I did this, I saw an issue pop up wherein javascript errors were thrown every time I opened a page that:
Message: Syntax error
Line: 3
Char: 1
Code: 0
URI: http://(domain)/WebResource.axd?d=3sB1WgLxUgrovkMyz-aqWw4&t=633626442871064790

... along with 2 errors for the ScriptResource.axd file and 2 instances of a 'Sys' is undefined error.
After some research, I found that this is caused by the combination of AJAX and URL Rewrite. I'm not sure if it is specific to GoDaddy hosting, or global. It's strange that it didn't surface until I moved my project. Regardless, I have found a solution.

The major problem is that my application is not recognizing the WebResource.axd and ScriptResource.axd as files; therefore tying to rewrite their urls using them as keywords.

My current URL Rewrite structure looks like this:
<rewrite>
<rules>
<rule name="RewriteCheck" stopProcessing="true">
<match url="^(.+)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}"
matchType="IsFile"
negate="true" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
negate="true" />
</conditions>
<action type="Rewrite" url="UserProfile.aspx?key={R:0}" />
</rule>
</rules>
</rewrite>


I'm already checking for files and directories, but now I need to check for these .axd files before casting a url into the rewriter. Therefore, I'll use the bounce URLs off a pattern *.axd to negate them from being processed by the Rewrite function.
<rewrite>
<rules>
<rule name="RewriteCheck" stopProcessing="true">
<match url="^(.+)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}"
matchType="IsFile"
negate="true" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
negate="true" />
<add input="{URL}"
pattern=".*.axd$"
negate="true" />
</conditions>
<action type="Rewrite" url="UserProfile.aspx?key={R:0}" />
</rule>
</rules>
</rewrite>


I re-ran my application and every worked like a charm!