refreshing data in datagridview

mcfly

Well-known member
Joined
Jun 15, 2009
Messages
54
Programming Experience
Beginner
Hi all,

I am having trouble with the data grid view. I have managed to get it to display the data in my table which is pulled from MS Access, but everytime I add a new record it wont display the new record. I had 8 rows in my database I have added at least three or four but it still shows the original 8 rows. I have checked the database and the new rows have been added no problem.

Heres the code for the data grid view. I only need this to display the data:

Used on the form load event
VB.NET:
 Me.Tbl_addressTableAdapter.Fill(Me.Hotel_dbDataSet.tbl_address)

I have closed and opened the program and tried ways of refreshing the data grid view but I don't think I have configured it correctly. If keeps disaplaying the same data as when I first opened it. I am using VS 2008.

Heres the code I use for adding a row to my table, which works:



VB.NET:
Private Sub frmSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmSave.Click

        Dim connetionString As String
        Dim oledbCnn As OleDbConnection
        'Dim oledbCmd As OleDbCommand
        Dim sql As String

        sql = "INSERT INTO tbl_address (person_id, type, add_1, add_2, add_3, town, county, postcode, tel_no, mob_no, email, supplier_name, fname, lname, notes) "

        sql = sql & "values(@person_id, @type, @add_1, @add_2, @add_3, @town, @county, @postcode, @tel_no, @mob_no, @email, @supplier_name, @fname, @lname, @notes) "

        'todo set password and user for database
        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=H:\My Pictures\hotel_db.mdb;User Id=admin;Password=;"

        oledbCnn = New OleDbConnection(connetionString)

        Try
            oledbCnn.Open()

            'open and prepare connection
            Dim command As New OleDbCommand(sql, oledbCnn)

            Dim person_type As String

            person_type = "BOTH"

            If chkSupplier.Checked = True And chkCustomer.Checked = True Then
                person_type = "BOTH"
                txtPersonType.Text = "BOTH"
            End If

            If chkSupplier.Checked = True And chkCustomer.Checked = False Then
                person_type = "SUPPLIER"
                txtPersonType.Text = "SUPPLIER"
            End If

            If chkSupplier.Checked = False And chkCustomer.Checked = True Then
                person_type = "CUSTOMER"
                txtPersonType.Text = "CUSTOMER"
            End If

            txtPersonType.Text = person_type

            command.Parameters.AddWithValue("@person_id", OleDbType.VarChar).Value = txtPersonID.Text
            command.Parameters.AddWithValue("@type", OleDbType.VarChar).Value = txtPersonType.Text
            command.Parameters.AddWithValue("@add_1", OleDbType.VarChar).Value = txtAdd1.Text
            command.Parameters.AddWithValue("@add_2", OleDbType.VarChar).Value = txtAdd2.Text
            command.Parameters.AddWithValue("@add_3", OleDbType.VarChar).Value = txtAdd3.Text
            command.Parameters.AddWithValue("@town", OleDbType.VarChar).Value = txtTown.Text
            command.Parameters.AddWithValue("@county", OleDbType.VarChar).Value = txtCounty.Text
            command.Parameters.AddWithValue("@postcode", OleDbType.VarChar).Value = txtPostcode.Text
            command.Parameters.AddWithValue("@tel_no", OleDbType.VarChar).Value = txtTelNo.Text
            command.Parameters.AddWithValue("@mob_no", OleDbType.VarChar).Value = txtMobNo.Text
            command.Parameters.AddWithValue("@email", OleDbType.VarChar).Value = txtEmail.Text
            command.Parameters.AddWithValue("@supplier_name", OleDbType.VarChar).Value = txtSupplier.Text
            command.Parameters.AddWithValue("@fname", OleDbType.VarChar).Value = txtFName.Text
            command.Parameters.AddWithValue("@lname", OleDbType.VarChar).Value = txtLName.Text
            command.Parameters.AddWithValue("@notes", OleDbType.VarChar).Value = txtPersonNotes.Text
            command.ExecuteReader()

            command.Parameters.Clear()

            oledbCnn.Close()

            connetionString = ""
            oledbCnn.Close()
            sql = ""

            MsgBox(person_type & " added")
            frmStart.Show()
            Me.Close()
        Catch ex As Exception
            'get error message
            MsgBox(ex.GetType.Name & " : " & ex.Message)
        End Try
    End Sub

This is code is used on text boxes above the data grid view.

It is almost like the data grid view has taken a snapshot of the database when i first set the thing up and doesn't diaplay any rows i have added after???? Any ideas how I refresh it? I would have thought that every time I reload the form or program the data grid view would show the newly added rows?!!? When the code above is called that adds a new row the form is closed, therefore, when I reload the form or the program the new data or new row should be seen?!?! should'nt it, I am new to this and never used the data grid view before, seemed easily to set up and the code is spit out onto the form but doesn't refresh when new data is added. may be the way I have set the thing up....any ideas??

Please can some body help :(
 
Replace

VB.NET:
command.ExecuteReader()

with

VB.NET:
command.ExecuteNonQuery()
 
have now added that cheers, but I still can't seem to get the data grid to display records that are added..any ideas:confused:
 
Hi there,

Those videos are really helpful espically for me who is a bit of a newie, going back to the original problem, when you create the data gird view in MS visual studio who can either copy the database from the current location to your project folder or leave the database where its to and then connect. I had copied the database but was still updating the version that wasn't within my directory. Hence no actual error just updating the wrong version

Basic School Boy Error...doh!!!

Cheers for the advice and links anyway guys :D
 
Back
Top