Solved

Change an image in asp.net while stored procedure is running

Posted on 2013-12-18
2
343 Views
Last Modified: 2014-01-02
I have a stored procedure that is run from a single button click on an aspx page. I have the image change at the end when the stored procedure runs successfully. I want to change the image also WHILE the stored procedure is running. The image I want to change to is an animated gif.
    protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        btnSubmit.ImageUrl = "~/images/cog.gif";

    try
    {
        System.Data.SqlClient.SqlConnection oConn = new System.Data.SqlClient.SqlConnection();
        System.Data.SqlClient.SqlCommand oCmd = new System.Data.SqlClient.SqlCommand();
        oConn.ConnectionString = ConfigurationManager.ConnectionStrings["man_pilotConnectionString"].ConnectionString;
        oConn.Open();
        oCmd.Connection = oConn;
        oCmd.CommandText = "aer_deactivate_recipes";
        oCmd.CommandType = CommandType.StoredProcedure;
        oCmd.Parameters.Add("@user", SqlDbType.VarChar, 25).Value = Label2.Text;
        oCmd.ExecuteNonQuery();
        oConn.Close();

        System.Data.SqlClient.SqlConnection oConn2 = new System.Data.SqlClient.SqlConnection();
        System.Data.SqlClient.SqlCommand oCmd2 = new System.Data.SqlClient.SqlCommand();
        oConn2.ConnectionString = ConfigurationManager.ConnectionStrings["fin_pilotConnectionString"].ConnectionString;
        oConn2.Open();
        oCmd2.Connection = oConn2;
        oCmd2.CommandText = "AER_UPDATE_PART_UOM_CONVERSIONS";
        oCmd2.CommandType = CommandType.StoredProcedure;
        oCmd2.ExecuteNonQuery();
        oConn2.Close();

        btnSubmit.ImageUrl = "~/images/Halo.gif";
        Label1.Text = "Wasn't that easy??";

    }
        
    catch (Exception ex)
        {
            btnSubmit.ImageUrl = "~/images/failure.jpg";
            Label1.Text = "Woops. Something went awry: " + ex.Message;
        }
    }

Open in new window

The image at the end changes successfully when the stored procedure is finished, but it takes several seconds for the sproc to run. I put the image change as the first action on the button click, before the sproc starts. I have also tried putting as the first action under the try block. What am I missing to get this image to change while the stored procedure is running?

Ultimately, I also want to get the page to play a sound when everything is finished. This is an application that is run once per year on January 1st and I try to make it "fun" and "different" every year since they have to work on the 1st.
0
Comment
Question by:Hers2keep
2 Comments
 
LVL 51

Accepted Solution

by:
Ted Bouskill earned 500 total points
ID: 39728537
OK, I think there is a misunderstanding about how web pages work.

When the user presses a button, the page request is submitted to the server.  The browser then waits for the server to process the request.  Your code and the stored procedure are executed before any results are sent back to the client.  What you have to understand is that there actually isn't an active streaming connection between the server and browser while the page is being processed on the server.  The browser won't show the page until the server is done processing the page and sends the results back to the browser.

What you are trying to do is not how web pages were designed!

Your results can be achieved but it's really tricky to do using some clever JavaScript and an IMG tag that executes an ASPX page instead of retrieving an image.  Many people don't realize that the src for an IMG tag can actually execute a page on the server.
<img src="http://mysite/mywebpage.aspx" />

Or you can look at using the UpdateProgress control in AJAX.

Sorry but it would take me an hour to write out a detailed example and I don't have time to do it.  I'm a volunteer.  I can point you in the right direction, however you need to find a good example on the web yourself.
0
 

Author Comment

by:Hers2keep
ID: 39751758
I found a work-around to achieve almost the same thing as I originally wanted. The only difference was I didn't change the image WHILE the query was running, but only changed it after it finished.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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