How to effect an automatic update on windows form

PRAISE PHS

Well-known member
Joined
Jun 2, 2011
Messages
58
Programming Experience
Beginner
Hi forumites,
Please I noticed that whenever I save details of an item on my windows form on sqlserver database. I noticed that I don't see the save details until after have closed the windows form and run it again. Pls how do I do it that I can see the saved details without having to close the form and run it again. My vb.net codes are below:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If cboItemSerialNo.Text = "" Then
                MessageBox.Show("Please Fill All Fields", "ALERT PROMPT", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf cboItemName.Text = "" Then
                MessageBox.Show("Please Fill All Fields", "ALERT PROMPT", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf txtPartNo.Text = "" Then
                MessageBox.Show("Please Fill All Fields", "ALERT PROMPT", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf txtSellingPrice.Text = "0.00" Then
                MessageBox.Show("Please Fill All Fields", "ALERT PROMPT", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf txtSellingPrice.Text = "" Then
                MessageBox.Show("Please Fill All Fields", "ALERT PROMPT", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf txtCostPrice.Text = "0.00" Then
                MessageBox.Show("Please Fill All Fields", "ALERT PROMPT", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf txtCostPrice.Text = "" Then
                MessageBox.Show("Please Fill All Fields", "ALERT PROMPT", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf cboQuantity.Text = "0.00" Then
                MessageBox.Show("Please Fill All Fields", "ALERT PROMPT", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf cboQuantity.Text = "" Then
                MessageBox.Show("Please Fill All Fields", "ALERT PROMPT", MessageBoxButtons.OK, MessageBoxIcon. Error)
                Exit Sub
            Else
                Adapter = New SqlDataAdapter("SELECT * FROM Products", MyConn)
                scb = New SqlCommandBuilder(Adapter)
                ds = New DataSet

                Adapter.Fill(ds, "Products")

                Dim DT As DataTable = ds.Tables(0)
                Dim DR As DataRow = DT.NewRow

                DR("ItemSerialNo") = cboItemSerialNo.Text
                DR("ItemName") = cboItemName.Text
                DR("PartNo") = txtPartNo.Text
                DR("SellingPrice") = txtSellingPrice.Text
                DR("CostPrice") = txtCostPrice.Text
                DR("Quantity") = cboQuantity.Text

                DT.Rows.Add(DR)
                Adapter.Update(ds, "Products")

                MessageBox.Show("Product Details Saved")
            End If
        Catch ex As Exception
            'MessageBox.Show(ex.Message)
            Exit Sub
        Finally
            MyConn.Close()
            Adapter = Nothing
            scb = Nothing
            ds = Nothing
        End Try
    End Sub
 
Last edited by a moderator:
For future reference, please take the time and make the effort to post code that is as readable as possible. With no formatting and loads of blank lines in it, it's a pain for us to read and, as a result, also reduces your chances of getting the help you need. I have cleaned up your code for you this time. Please do it for us in future.

As for the question, the issue is that, just before making your changes, you get a new set of data from the database. Presumably you already have a populated DataTable so you should be using that, not creating a new one and populating that with data that you already have. The procedure should be something like this:

1. Query the database and populate a DataTable when the form opens using a data adapter.
2. Edit the data as required, i.e. insert, update and delete records as required.
3. Use the same data adapter to save all the changes from the DataTable back to the database.

If you are clicking a Save button then you should already have made the changes to save, so you shouldn't be creating any data adapters or DataTables at that late stage. You should be using the ones you created earlier. For an example, check this out:

Retrieving and Saving Data in Databases
 

Latest posts

Back
Top