sgl
sgl

Reputation: 27

Refreshing page while procedure is executed

I created an interface to pull data, there are a few simple steps.

Users select their desired data from drop boxes, they click a "Check Available" button, and if there are records available, a "Send Request" button shows.

After clicking the send request button, SQL tables are sent to an excel file, the file is slightly massaged with vba, then the user is emailed from C# with a link to this excel file.

The issue I'm facing is that the users get impatient after one or two seconds and start mashing the "send Request" button over and over again. This impatience crashes the application.

How could I make the page behave so that after clicking "Send Request", the page is refreshed but the process is still carried out over the server and the email is still sent from C#.

public void SendRequest_Click(object sender, EventArgs e)
{    
    //Insert amazing line of code that refreshes the web form but does everything beyond this line

    SendRequest.Visible = false;
    //Creating a new class
    QueryMule mule = new QueryMule();
    Label11.Visible = true;
    string myfilename = Label3.Text;
    string splitusernum = Label4.Text;
    string dtablenum = Label4.Text;
    string exportnum = Label4.Text;
    //Export all export tables to mercuryexport excel file
    mule.SQLExport(18, splitusernum, exportnum);
    //Creating a new excel application to open the mercury template and run its macro
    ExcelApp.Application appExl;
    ExcelApp.Workbook workbook;
    ExcelApp.Worksheet newSheet;
    ExcelApp.Range Range;
    appExl = new ExcelApp.Application();

    RegionName.SelectedItem.Value + timestampeddone + ".xlsm";

    workbook = appExl.Workbooks.Open(@"filepath", Missing.Value,
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, 
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
        Missing.Value, Missing.Value, Missing.Value);

    appExl.Visible = true;
    appExl.DisplayAlerts = true;

    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    message.To.Add '''REMOVED EMAIL ADDRESS
    message.Subject = "Delivery System";
    message.From '''REMOVED EMAIL ADDRESS
    message.Body = "filepath" + myfilename;
    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mailhost");
    smtp.Send(message);

    Label4.Text = "Email Sent";

    mule.DropTable(dtablenum);
    mule.DropTable1(dtablenum);
    mule.DropTable2(dtablenum);
    mule.DropExportTable(dtablenum);
}

Upvotes: 2

Views: 149

Answers (2)

S. Ravi Kiran
S. Ravi Kiran

Reputation: 4303

Your logic includes two heavy-weight operations: Generating excel file and sending mail. You may use a background thread as Paul suggested to make the server processing lighter.

Otherwise, block the page until you get response from the server so that the user will not be able to interact with the page until the page is ready. You can do this using jQuery block UI plugin (http://jquery.malsup.com/block/#).

A sample usage of this plugin is shown in an ASP.NET forum post: http://forums.asp.net/p/1656559/4317158.aspx/1?Re+show+progress+in+dialog

Upvotes: 0

Paul Grimshaw
Paul Grimshaw

Reputation: 21034

You'd need to spin off a background worker thread to create the excel file. The request would then respond immediately with a friendly message indicating that they will be emailed the link when it is ready.

Upvotes: 1

Related Questions