Problem refreshing databound listbox

shalan99

Active member
Joined
Jun 1, 2007
Messages
32
Programming Experience
Beginner
This is my first post in the vb.net forums, so HI everyone!

I currently have a vb windows application. In one form I have a listbox that on Form_Load displays a particular field from a database table. Upon clicking on any item in the listbox, it will populate nearby labels with further info about the selected item.

On the same page is an option to add an additional item, modify existing items and delete existing items, which I am able to do successfully by calling respective sql stored procedures.

My problem is that I can't find a way to refresh the listbox after any item has been modified, added or deleted. You can imagine the confusion that arises when a user selects an item, chooses to delete it, it pops up a message stating that the item has been deleted....but it still shows in the listbox! the only way for a user to notice any change is to close the form and re-open it, and I was hoping that there was a simpler way whilst still having that form open. I tried: ListBox1.Refresh() but I dont think that method is intended for what I want it to do.

Your help is greatly appreciated. Much Thanx!

Regards
Shalan99
 
Oh, also...I have tried to clear the listbox by using: ListBox1.Items.Clear() and then follow up immendiately with a re-read into the listbox but I got an error saying something to the effect of the items in the listbox cannot be cleared whilst bound to a s DataSource.
 
Your listbox is showing the contents of a datatable. You should add/modify/delete the contents of that table, then use the TableAdapter.Update() method to sync the database..

If none of this makes sense, read the DW2 link in my signature, section on SImple Data App
 
Hi cjard, thanx for the help. I'm printing out out the articles so that I can read them and (hopefully) implement them successfully at home.

If I'm still at a loss, may I post my code here for further assistance?
 
Hi cjard,

I was a bit overwhelmed reading those articles...guess its a Friday so my mind is on other things! But I thought to make it easier to communicate my intended result, I'd also include a small snapshot of the form itself. (attached)

My code for the page is given below (to save reading, i just pasted the Load and Insert procedures here as examples):
VB.NET:
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmdSave.Visible = False
        Call LoadEmployees()
    End Sub

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        Call InsertNewEmployee()
    End Sub

    Public Sub LoadEmployees()
        Dim conn As New SqlConnection(My.Settings.ConnectionString)
        Dim TSql As String = "SELECT EmpID, EmpName, EmpEmail, EmpPhone, EmpDept FROM Employees"
        Dim da As New SqlDataAdapter(TSql, conn)
        da.Fill(DataSet1, "Employees")
        Dim dt As DataTable = DataSet1.Tables("Employees")
        ListBox1.DataSource = dt
        ListBox1.DisplayMember = "EmpName"
        txtEmployeeID.DataBindings.Add("text", dt, "EmpID")
        txtName.DataBindings.Add("text", dt, "EmpName")
        txtEmail.DataBindings.Add("text", dt, "EmpEmail")
        txtPhone.DataBindings.Add("text", dt, "EmpPhone")
        txtDepartment.DataBindings.Add("text", dt, "EmpDept")
    End Sub

    Public Sub InsertNewEmployee()
        Dim conn As New SqlConnection(My.Settings.ConnectionString)
        conn.Open()
        Try
            Dim cmd As SqlCommand = New SqlCommand("InsertEmployee", conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = txtName.Text
            cmd.Parameters.Add("@EmpEmail", SqlDbType.VarChar).Value = txtEmail.Text
            cmd.Parameters.Add("@EmpPhone", SqlDbType.NChar).Value = txtPhone.Text
            cmd.Parameters.Add("@EmpDept", SqlDbType.VarChar).Value = txtDepartment.Text
            cmd.ExecuteNonQuery()
            MsgBox("created!")
        Catch ex As Exception
            MsgBox(ex.ToString())
            Throw
        Finally
            conn.Close()
        End Try
    End Sub
Somewhere in the midst of this code, either in the InsertNewEmployee Sub or in the click event of cmdAdd, I want to tell ListBox1 to clear itself, and then call the LoadEmployees Sub, so that the ListBox is refreshed without the user having to close and then re-ope the form.

Thanx again cjard, I really appreciate your time and help!
kind regards
Shalan99
 

Attachments

  • form.jpg
    form.jpg
    13 KB · Views: 26
Last edited by a moderator:
also, I know the form isn't aesthetically pleasing, but I want to get the functionality right before styling it!
 
Looks pretty nice to me.. :)

Youre doing data access the hard way though.. DO have another read of the article, because when it sinks in, you'll think "oh wow.. that's so simple"

Start with "a simple data app" and move onto the walkthroughs involving stored procedures
 
thanx, cjard! I guess I DO need more time with them. I'll start from the beginning of the walkthroughs and work my way slowly thru them.

Cheers!
Shalan99
 
Back
Top