tags:

views:

97

answers:

5

Hi,

I have a code in my asp.net page where I am inserting some data in the database while uploading a file to the server. The problem is, it seems that the application is waiting for the file to be uploaded before it will insert to the database. Below is a code similar to mine.

public partial class _Default : System.Web.UI.Page
{
    protected HtmlInputFile XLSFileInput;

    ...

    protected void ImportButton_Click(object sender, EventArgs e)
    {
        InsertToDatabase(); //method to insert to database
        XLSFileInput.PostedFile.SaveAs(filePath + fileName);   
    }

    ...
}

The problem here is that it seems that the InsertToDatabase() method is executing only after the file is uploaded to the server. Any help is appreciated.

+1  A: 

That's generally how single threaded applications work.

From your code I am guessing your using ASP.NET Web Forms.

You would have to consider sending the InsertToDatabase() operation off to free up the program to do your file upload. Perhaps depending upon your version consider UpdatePanels as a quick and dirty way to achieve this?

You suggest these operations are separate, provide more details to help figure out what each task does and if JavaScript is possible.

But your code sample indicates that InsertToDatabase() should be going before the file save.

If you are unsure of AJAX/JavaScript you could use a Thread pool to do your InsertToDatabase() method. However it all depends on what that method does. Please provide more code/details. I personally use this for a database insert which happens in the background (A logging Action Filter in ASP.NET MVC so other users may disagree on the validity of this usage. However it might save you learning another language.

ThreadPool.QueueUserWorkItem(delegate
 {
   // Code here
 }
);
Phil
Sorry if I keep repeating myself on this question, but this is not a threading issue. As such, threading is not the solution.
Jeromy Irvine
I disagree on threading not being a solution. I personally use it myself. But as the question asker is unsure of JavaScript or AJAX I originality thought a server side solution would be more acceptable.I have now revised my answer. UpdatePanels might be the best solution as he has now stated he/she does not know JavaScript.
Phil
The uploaded file is part of the HTTP post. The post, and thus the upload, is going to take place before any server code is run, and that includes the thread-spawning code. Spawning a thread to do the database insert could actually result in the file being uploaded *and* saved to disk before the insert is completed. That takes the OP even farther from his desired behavior.
Jeromy Irvine
A: 

You could start another thread for the database insert. For Example:

ThreadStart job = new ThreadStart(InsertToDatabase);
Thread thread = new Thread(job);
thread.Start();
Matthew Sposato
I tried doing this but it did'nt work. :(
Walter
This is not a threading issue, it's an HTTP post issue. The code to run the database insert in a thread is still going to fire after the file is already uploaded as part of the HTTP post.
Jeromy Irvine
+2  A: 

I would not recommend trying to manually control threading in ASP.NET, that is a job best left purely to IIS.

IMO for ASP.NET the better way to handle this is either invoke these requests through either multiple AJAX operations from the browser or to setup a WCF service that supports one way operations so when you call "InsertToDatabase" it executes a fire and forget operation to the WCF service sitting ontop your database that executes immediately and then continues onto the next line of code. Then IIS is running the code that the service method calls in it's own thread.

IMO using WCF is one of the most appropriate ways of handling threading in ASP.NET. Since you can easily set any service method to be synchronous or asynchronous.

Edit:

Introduction to Building Windows Communication Foundation Services

What You Need To Know About One-Way Calls, Callbacks, And Events

Chris Marisic
Do you have a sample code for this? or probably some resources that I can read?
Walter
Added some links about WCF that should help you get started
Chris Marisic
What was the DV for?
Chris Marisic
+1  A: 

The file upload is a part of the HTML form post. Since the upload is a part of the form post process, it's always going to happen before any of your server-side code executes. The PostedFile.SaveAs() call doesn't cause the upload to happen, it just saves what was already uploaded as part of the request.

If you absolutely need the database insert to happen before the upload begins, you could do as @Chris Marisic suggests and run the insert as an AJAX call prior to submitting the form.

Jeromy Irvine
Can you give me some sites where I can read about AJAX. im totally innocent to that language. Or probably some sample codes would be great. Thank you.
Walter
@Walter - This might help get you started http://www.asp.net/learn/ajax-videos/
Jeromy Irvine
+2  A: 

This has NOTHING to do with the IIS (webserver) threading, it's more of a HTTP "problem".

The file is selected on the client and then all response data (including the file) is posted to the server before any server code is ran.

So the file is uploaded but not saved before the InsertToDatabase(); is executed.

This behaviour can only be worked around doing several posts (eg. with ajax) and is probably not a god solution for you.

Tell us more about what you are trying to accomplish and we might come up with some better suggestions :).

bang