Search code examples
vb.netdatagridviewfoxprovisual-foxpro

VB.NET 2008 DataGridView not Updating Visual Foxpro Database


I'm using VB.NET 2008 with a DataGridView and I'm interfacing to a Visual Foxpro 6 database using the vfpoledb.1 driver. When I change a value in the description field, it changes in the DataGridView but the database never gets updated. Do I need to use code to force the changes to take place?

Here's the code I'm using:

Imports System.Data.OleDb

Public Class Form1
    Dim sConString As String = "Provider=vfpoledb.1;Data Source=C:\MyDatabase.dbc;Mode=3;"
    Dim con As OleDbConnection = New OleDbConnection(sConString)

    Private Function FetchData()

        con.Open()
        Dim ds As DataSet = New DataSet()
        Dim sSQL As String
        sSQL = "SELECT item_cd, item_desc FROM invent;"

        Dim cmd As OleDbCommand = New OleDbCommand(sSQL, con)
        Dim daInv As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        Dim iRecCount As Integer
        iRecCount = daInv.Fill(ds, "invent")
        Me.DataGridView1.DataSource = ds.Tables("invent").DefaultView
    End Function

    Private Sub btnFetchData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFetchData.Click
        Call FetchData()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        con.Close()
        con = Nothing
    End Sub
End Class

Solution

  • I found the underyling problem and resolved it. The VFP table I'm accessing does not have a primary key defined. It does have indexes but they are simply marked as "Regular" indexes (using VFP terminology).

    I was able to use a VFP tool call vRunFox (Visual Run Fox) from Ed Leafe's website to modify the table structure. I had to use some VFP commands to bring up the Table Designer window.

    I also had to add some code to my FormClosing event. Here's what my FormClosing event looks like now:

        Dim myBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(daInv)
        myBuilder.GetUpdateCommand()
        daInv.UpdateCommand = myBuilder.GetUpdateCommand
        daInv.Update(ds.Tables("invent"))
        con.Close()
        con = Nothing