BeginExecuteNonQuery - call a procedure without waiting for it to complete

Posted on 2014-07-16
Last Modified: 2014-07-18
I am running a query in sql that takes many minutes to execute. I want to call the query from my website and the user should not have to wait for it to finish, i.e. as soon as he gives the command to execute the query he should be able to go to do somehting else on my website. I am trying to use BeginExecuteNonQuery.
With following code I do not call the pocedure. Do you know what I am doing wrong?

public void test_T1_Insert(string t1)
        SqlCommand command = new SqlCommand("test_T1_Insert", connection);
        command.CommandType = CommandType.StoredProcedure;

        command.Parameters.Add("@T1", SqlDbType.VarChar);
        command.Parameters[0].Value = t1;
Question by:johnson1
    LVL 21

    Assisted Solution

    by:Randy Poole
    Unfortunately the page must stay open until the query is completed.  You could utilize html frame with a hidden frame that can spawn iframes which runs these types of queries or you can utilize a run table with the SQL Server Agent which can then query the table and run the needed operation.  You can find more on this command and how it works here:
    LVL 68

    Accepted Solution

    You could also create a job to run the query, then start the job.  When you run sp_start_job, SQL just starts the job and returns immediately to the caller.  You can set the job to delete itself, to prevent jobs from building up in SQL Agent.

    Author Closing Comment

    Thank you.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now