Datagridview save changes to Database vb.net
69,859
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:
con.Open()
Dadapter.Update(DSet, "Table1")
DSet.AcceptChanges()
con.Close()
with
Using con = New OleDbConnection(myConString)
con .Open()
Dadapter.Update(DSet, "Table1")
End Using
Author by
FPGA
Updated on December 22, 2020Comments
-
FPGA over 3 years
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