FPGA FPGA - 5 months ago 92
Vb.net Question

Datagridview save changes to Database vb.net

Hello i've a databse that i load to Datagridview in vb.net application.
it loads fine, however when i try to save the date it doesn't work.
here is the code

Private myConString As String
Private con As OleDbConnection = New OleDbConnection
Private Dadapter As OleDbDataAdapter
Private DSet As DataSet
Private DSet2 As DataSet
Private ConCMD As OleDb.OleDbCommand

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
myConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=e:\Database31.accdb"
con.ConnectionString = myConString
Dadapter = New OleDbDataAdapter("select * from Table1", con)
DSet = New DataSet
Dadapter.Fill(DSet, "Table1")
DataGridView1.DataSource = DSet.Tables("Table1")
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dadapter.Update(DSet, "Table1")
End Sub

Update requires a valid InsertCommand when passed DataRow collection with new rows.
what am i supposed to do?

the accessdatabase 3 colmuns and ID is primary key
ID Field1 Field2


So you must define an InsertCommand for you DataAdapter

Side-note: The line DSet.AcceptChanges() is redundant since the previous line Dadapter.Update will call AcceptChanges implicitely.

You should use using-statement for anything implementing IDisposable like a Connection. That would call Dispose(which closes the connection) implicitely even in case of an exception.

So replace:

Dadapter.Update(DSet, "Table1")


Using con = new New OleDbConnection(myConString)
    con .Open()
    Dadapter.Update(DSet, "Table1")
End Using