Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8966295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:59:42+00:00 2026-06-15T16:59:42+00:00

VB.NET 2.0: I’m issuing a backup command to a SQL server from a VB.NET

  • 0

VB.NET 2.0:

I’m issuing a backup command to a SQL server from a VB.NET application. I’m capturing the messages it sends out and appending them to a multiline textbox.

What I’m trying to do, though, is to allow the application to keep responding to GUI control events (mainly so that the user can resize the output window or cancel the backup). So I use BeginExecuteReader and spin in a loop with Application.DoEvents(). But it seems that as soon as the backup starts issuing its print statements, I get IAsyncResult.IsCompleted = True and it drops down to EndExecuteReader, and the GUI is now locked up again. How can I get it to stay in the loop until the backup command completes and yet still get those output statements and keep the GUI responsive? Thanks.

Here’s my code:

' Enable retrieve print statements from the server'
AddHandler oConn.InfoMessage, AddressOf LogToBufferHandler

strSQL &= vbNewLine & "begin try"
strSQL &= vbNewLine & " declare @BackupName as varchar(255)"
strSQL &= vbNewLine & " declare @BackupDesc as varchar(255)"
strSQL &= vbNewLine & " declare @backupTime as varchar(50)"
strSQL &= vbNewLine & " set @backupTime = (select convert(datetime, getdate(), 100))"
strSQL &= vbNewLine & " set @BackupName = (SELECT '[' + db_name() + '] Full Backup')"
strSQL &= vbNewLine & " set @BackupDesc = (SELECT 'Automated full backup of [' + db_name() + '] on ' + @backupTime + '.')"
strSQL &= vbNewLine & " "
strSQL &= vbNewLine & " BACKUP DATABASE [#Database#]"
strSQL &= vbNewLine & " TO DISK = @BackupFullPath"
strSQL &= vbNewLine & " WITH stats,"
strSQL &= vbNewLine & "      NAME = @BackupName,"
strSQL &= vbNewLine & "      DESCRIPTION = @BackupDesc;"
strSQL &= vbNewLine & " select [IsSuccessful] = 1"
strSQL &= vbNewLine & " end try"
strSQL &= vbNewLine & " begin catch"
strSQL &= vbNewLine & " SELECT [IsSuccessful] = 0"
strSQL &= vbNewLine & " end catch"

'Workaround: Backup Database requires the name of the object, not a string'
'            and I dont want to use dynamic SQL.'
strSQL = strSQL.Replace("#Database#", sb.InitialCatalog)

oConn.Open()
oCmd = New SqlCommand()
oCmd.Connection = oConn
oCmd.CommandText = strSQL
oCmd.CommandType = CommandType.Text
oCmd.Parameters.AddWithValue("@BackupFullPath", backupFullPath)
oCmd.CommandTimeout = 60 * 5

'Spin until complete, cancel, or timeout'
Dim result As IAsyncResult = oCmd.BeginExecuteReader(CommandBehavior.CloseConnection)
While Not result.IsCompleted
    Application.DoEvents()
    If blnCancel Then
        oCmd.Cancel()
    End If
    System.Threading.Thread.Sleep(50)
End While
Try
    oDataReader = oCmd.EndExecuteReader(result)
    oDataTable.Load(oDataReader)

    'Get results'
    ' (unfourtunately, you cannot do BeginExecuteScalar ASync in .Net 2.0,'
    ' so we are using a DataTable first column, row)'
    If oDataTable IsNot Nothing _
    AndAlso oDataTable.Rows.Count > 0 _
    AndAlso oDataTable.Columns.Contains("IsSuccessful") _
    AndAlso oDataTable.Rows(0).Item("IsSuccessful") = 1 Then
        eBackupResult = BackupStatus.Succeeded
        returnPath = backupFullPath 'Only set return path if the backup succeeded'
    Else
        eBackupResult = BackupStatus.Failed
    End If
Catch ex As Exception
    If Not ex.Message.Contains("cancelled by user") Then Throw ex
    eBackupResult = BackupStatus.Canceled
End Try
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:59:43+00:00Added an answer on June 15, 2026 at 4:59 pm

    Polling loops (DoEvents) are considered evil for many reasons. It is probably the easiest way for you to switch to BackgroundWorker and abandon the nasty Begin-poll-End scheme.

    If you want to keep it, here is the bug: Accepting the reader can be quick but that doesn’t mean that all results have arrived (let me illustrate: what if you had queried 1TB of data? – reading that is a long process). You need to Read in an async (polling) way too. Now the thing gets out of hand. Just abandon it.

    In other words, oDataTable.Load is blocking which is hard to fix.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

.NET 4.0 - C# Application needs to pull messages out of Exchange Server 2007.
net with vb and sql server 2005. i have DataSet with 3 tables in
Net MVC 3 web application. I generated my models from an existing database using
net I am trying to take a value from a previous form typically in
.NET provides EventLog class that be able to write application event information to a
.NET profilers can show reference count to managed objects. How do they count them?
.Net 4 WPF DataGrid MVVM User clicks add button which triggers command on viewmodel.
net to make a Windows Phone application. I have a listBox on page1 called
net application, in which i am using some popup windows to open some pages.
NET web application and a WCF Application that share the same ASP.NET membership database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.