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
con.Open()
Dadapter = New OleDbDataAdapter("select * from Table1", con)
DSet = New DataSet
Dadapter.Fill(DSet, "Table1")
DataGridView1.DataSource = DSet.Tables("Table1")
con.Close()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
con.Open()
Dadapter.Update(DSet, "Table1")
DSet.AcceptChanges()
con.Close()
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
InsertCommandfor youDataAdapterSide-note: The line
DSet.AcceptChanges()is redundant since the previous lineDadapter.Updatewill callAcceptChangesimplicitely.You should use
using-statementfor anything implementingIDisposablelike a Connection. That would callDispose(which closes the connection) implicitely even in case of an exception.So replace:
with