Click here to Skip to main content
15,670,482 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to increase my progress bar when a button is pressed in my c# application

I currently have it set out like this;
private void backupbutton_Click(object sender, EventArgs e)
try {
                progressBar1.Value = 30;
                conn = new SqlConnection(connectionString);
                sql = "BACKUP DATABASE " + combodatabase.Text + " TO Disk = '" + "C:\\Temp" + "\\" +manualbackupname.Text+"-"+ combodatabase.Text + "-" + DateTime.Now.Ticks.ToString() + ".bak'";
                command = new SqlCommand(sql, conn);
                progressBar1.Value = 100;  

The problem I have is I want the progress Bar value to be set at 30 before the script is run, but it doesn't adjust the progress bar until the SQL script is completed.

Not sure where I'm going wrong, anything anyone can suggest?

What I have tried:

I've tried moving the progressBar1.Value= 30; to different points in the 'try' section, but can't get it to work.
Updated 1-Apr-20 7:33am
F-ES Sitecore 31-Mar-20 8:06am    
Your code is running on the same thread as the UI is running on so while your code is running the UI won't update. What you need to do is run your code on a different thread in the click event, that will allow the UI to update while your SQL code is running. Note that you won't be able to access UI components from your new thread, you'll need to use Invoke to update the UI. Google for "winform new thread ui update" and you'll find examples of how to do this.

Three things:
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) When you execute a Click handler, no more changes will happen to the display until it exits: the UI thread is the only thing which executes the Paint event, and it's tied up running your Click handler code, then objects don't get Painted. So the first progress bar value setting won't do anything useful as it will never be seen.

3) You can move that code (or at least the fixed version) onto a separate thread, but then you can't access the controls such as the ProgressBar directly - trying to do so will cause a cross threading error as only the UI thread can have access to any control.

Try using a BackgroundWorker[^] - it provides a reporting mechanism designed for progress reporting.
Share this answer
You're better off executing the SQL code in a BackgroundWorker as already mentioned. However, you can "cheat" by adding this line after a change to any control to cause the UI to update. But I strongly don't recommend it, because the user could also close the containing Form when this line executes.
Application.DoEvents(); // allow the UI to update
Share this answer
If you want to report the real progress of the backup, you'll probably have to use SMO:
Backup Class (Microsoft.SqlServer.Management.Smo) | Microsoft Docs[^]
Backup and Restore SQL Server databases programmatically with SMO[^]

The alternative would be to use the InfoMessage event, as described in this StackOverflow thread:
c# - Backup SQL Server Database with progress - Stack Overflow[^]

In both cases, as previously mentioned, you would need to use a BackgroundWorker to allow your UI to update whilst the backup is in progress.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900