amberdextrous
MIS
This is probably going to sound like a really dumb question. In my program, the user selects values from 3 drop-down lists and hits the "Export" button, and the values are passed as parameters to a SQL Server 2005 stored procedure. The results of the query are stored in a dataset, which is then written to an XML file. I'm trying to get a label that says "Please wait..." to show up next to the button when the button is clicked, that will disappear when the operation is complete (that is, everything from opening the database connection to writing the XML file). But for some reason, whenever I hit the "Export" button, the label does not appear until the "File exported to specified directory" message pops up (which is after the operation is already complete). I even had another programmer look at this and even though it seems like such a simple matter, we can't figure out why this is happening. The label should show up when the event handler first starts to run, not right before it finishes.
Here is the code for the "Export" button's event handler. Label8 is my "Please wait..." label, and it's visibility property is set to "False" on the form:
Anyone know why this is happening and what I can do to solve it?
Here is the code for the "Export" button's event handler. Label8 is my "Please wait..." label, and it's visibility property is set to "False" on the form:
Code:
Public Class mainForm
Private Sub ExportButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportButton.Click
Me.Cursor = System.Windows.Forms.Cursors.Default
Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
'Here's where I try to make it appear, but it won't actually appear on the GUI until the end of this subroutine is almost reached
Label8.Visible = True
' Prepare the database connection.
Dim connectionString As String = My.Settings.FileMakerConnectionString
Dim theDatabase As New SqlConnection(connectionString)
Try
theDatabase.Open()
Catch ex As SqlException
MessageBox.Show("Error connecting to the database. File not exported.", "Error", MessageBoxButtons.OK)
Label8.Visible = False
Exit Sub
End Try
' Check for a valid date/time range
If EndDatePicker.Value < StartDatePicker.Value Then
MessageBox.Show("Invalid date/time range.", "Error", MessageBoxButtons.OK)
Label8.Visible = False
Exit Sub
End If
' Declare a SqlDataAdapter object.
Dim objDataAdapter As New SqlDataAdapter()
' Assign a new SqlCommand to the SelectCommand property.
objDataAdapter.SelectCommand = New SqlCommand()
' Set the SelectCommand Connection properties and the query to be executed.
objDataAdapter.SelectCommand.Connection = theDatabase
objDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
objDataAdapter.SelectCommand.CommandText = "SP2"
' Declare parameters for the query
objDataAdapter.SelectCommand.Parameters.AddWithValue("@clientname", NamePicker.SelectedValue)
objDataAdapter.SelectCommand.Parameters.AddWithValue("@startdate", StartDatePicker.Value)
objDataAdapter.SelectCommand.Parameters.AddWithValue("@enddate", EndDatePicker.Value)
' Declare a DataSet object.
Dim objDataSet As DataSet = New DataSet("TEST")
' Fill the DataSet object with data, or
' Catch error if user loses network connection/authentication
Try
objDataAdapter.Fill(objDataSet, String.Format(NamePicker.SelectedValue + "_Record"))
Catch ex As SqlException
MessageBox.Show("Error connecting to the database. File not exported.", "Error", MessageBoxButtons.OK)
Label8.Visible = False
Exit Sub
End Try
' If no matching records were found, notify the user.
Dim objTable As New DataTable
objTable = objDataSet.Tables(0)
If objTable.Rows.Count > 0 Then
Else
MessageBox.Show("No records were returned.", "Error", MessageBoxButtons.OK)
Label8.Visible = False
Exit Sub
End If
' Close the database connection.
theDatabase.Close()
' Set the file directory to which the exported file will be written
My.Computer.FileSystem.CurrentDirectory = textBox.Text
' Use this if you always want to save the file to the desktop
' Dim filepath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
' My.Computer.FileSystem.CurrentDirectory = filepath
' Convert selected DateTimePicker dates to strings
Dim startDate2 As String = StartDatePicker.Value.ToString("MM-dd-yyyy_HHmmss")
Dim endDate2 As String = EndDatePicker.Value.ToString("MM-dd-yyyy_HHmmss")
Dim clientName As String = NamePicker.SelectedValue.ToString
Dim filename As String = clientName + "_" + startDate2 + "_TO_" + endDate2 + ".xml"
' Write query results to XML file.
Try
objDataSet.WriteXml(filename)
Catch se As System.UnauthorizedAccessException
MessageBox.Show("You do not have the appropriate permission to take this action.", "Error", MessageBoxButtons.OK)
Label8.Visible = False
Exit Sub
Catch ex As Exception
MessageBox.Show("There was an error. File not created.", "Error", MessageBoxButtons.OK)
Label8.Visible = False
Exit Sub
End Try
'This is where I finally see the label appear, at the same time this messagebox appears
If filename.CompareTo(My.Computer.FileSystem.CurrentDirectory & filename) = 1 Then
MessageBox.Show("File exported to specified directory.", "Success", MessageBoxButtons.OK)
Else
MessageBox.Show("There was an error. File not created.", "Error", MessageBoxButtons.OK)
End If
'It disappears here, like it should
Label8.Visible = False
' Clean up
objDataAdapter = Nothing
objDataSet = Nothing
End Sub
Anyone know why this is happening and what I can do to solve it?