We help IT Professionals succeed at work.
Get Started

Trying to Refresh Data from SQL in List Box VB .Net

Karl_mark asked
Last Modified: 2013-12-26
I have a window in VB.net which contains data from a table in SQL Server using an sqladaptor. The user has an option to add data to the table by clicking a button and selecting the source data. The question is, how do I refresh the data in the listbox to reflect the addition of the data?
The data is added by opening another form. I thought that I could refresh the data by reflling the dataset by calling the getfocus. However, this does not refresh the data and teh only way I have succeeded in seeing the new data is by physically closing the form at runtime and then reloading it. The code I have for refreshing the data is:
Private Sub FillDataSetAnd View()
  objAdaptor = Nothing
  objAdaptor = New SqlDataAdaptor("Select identifier, CI Type, FilePath FROM dbo.usr_importfilepaths", objConnection)
  objDataSet = New DataSet
  lstFilePaths.DataSource = objDataView
  lstFilePaths.DisplayMember = "CIType"
End Sub

Open in new window

I've tried adding this on the GotFocus and the Load events for the form. However, these do not seem to fire, mainly because the form will not close. Ideally I'd like the form to close when the add new data button is selected, but I can't seem to get this to work.
I have a private sub in the gorm called ExitForm() which empties all variables and has Me.Close() as the final line. This does not close the form however! I've tried several methods of closing the form, including having code to close the form as part of the code on the button which calls the next form:

'Call next form and close this form
Dim frmAddCITypes as Form
frmAddCITypes = frmAddCI  'This calls the code at the top of the frmAddCI form under Public Class
ExitForm() 'private sub to close existing form

Open in new window

Because I can't get the first form to close correctly I can't call the load event on the first form when the data has been successfully added. What is the best way of closing one form, launching the next form or indeed updating a dataset to show additions?
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE