Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid refresh after record insert

Status
Not open for further replies.

jpinto

Technical User
Dec 12, 2003
75
PT
Hello,

I've the following code to insert a record into an MS Access database when clicking on Button1:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=""C:\Programaçao\Visual Basic\Inapal\ReportIt\ReportIt.mdb"";"
        con.Open()
        sSQL = "SELECT * FROM [Scrap]"
        da = New OleDb.OleDbDataAdapter(sSQL, con)
        da.Fill(ds, "Scrap")

        Dim dsNR As DataRow
        Dim cb = New OleDb.OleDbCommandBuilder(da)
        dsNR = ds.Tables("Scrap").NewRow
        dsNR.Item("Tipo_Peça") = ScrapCombo.SelectedValue
        dsNR.Item("Qtd") = Qtdfield.Text
        ds.Tables("Scrap").Rows.Add(dsNR)
        da.Update(ds, "Scrap")

        DataGrid.DataSource = ds
        DataGrid.DataMember = "Scrap"
        con.Close()

    End Sub

When I save one record, the DataGrid gets refreshed with the latest data;
When I save another record, the DataGrid repeats all the records again!

Can annyone help please?

Thanks in advance,

João Pinto

 
When you fill your dataset with this code:

sSQL = "SELECT * FROM [Scrap]"
da = New OleDb.OleDbDataAdapter(sSQL, con)
da.Fill(ds, "Scrap")

you do not create a new dataset. It seems you are reusing an existing dataset. When you do this, the Fill just adds records to what is already there. You need to either destroy then recreate the dataset, or clear the dataset before doing the fill:

sSQL = "SELECT * FROM [Scrap]"
da = New OleDb.OleDbDataAdapter(sSQL, con)
[red]ds.Clear()[/red]
da.Fill(ds, "Scrap")

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top