Help on webClient.DownloadFileCompleted method

Hi all,

I'm currently using webClient.DownloadFileAsync to download a file in my coding.

I had place the coding below to auto trigger a method upon download complete

webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

I have problem trying to assign a value to the label in Complete method
lb_show.Text = "Download Completed";
I would hit an error upon reaching that line

Object reference not set to an instance of an object.
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.NullReferenceException: Object reference not set to an instance of an object.

[NullReferenceException: Object reference not set to an instance of an object.]
youtubeWeb._Default.Completed(Object sender, AsyncCompletedEventArgs e) in C:inetpubwwwrootyoutubeWebyoutubeWebMain.aspx.cs:129
System.Net.WebClient.OnDownloadFileCompleted(AsyncCompletedEventArgs e) +22
System.Net.WebClient.DownloadFileOperationCompleted(Object arg) +48
System.Web.AspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state) +73

Appreciate that anyone could suggest any solution


  • It looks like you are doing this in an ASP site and I'm a little rusty on that right now. My guess is that your multi threading in a state-based environment so by the time that result method/thread finally hits, your object references no longer hold a value. So you are getting a null reference when you try to access your UI elements that have already posted to the screen and been dumped from server-side memory. What you might try doing is just storing the result data to a session variable or something persistent and have a timed post-back on your page to pick it up and put it into one of your controls.

    You might try posting this in the ASP forum for more help.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion