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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating from a datagridview 1

Status
Not open for further replies.

trimtrom

Programmer
Dec 12, 2003
35
0
0
GB
Hello

I am new to VB.net and have a form where I am trying to update an Access database from a datagridview that I databound on the fly. However when I change something on the grid and press the update button it would seem that the dataset has accepted the changes, but when I next look at the grid, it hasn't updated. Can I use a SELECT statement in the way that I have?

Am I doing something wrong in the code, or is there something in the grid properties that I need to change?

Many thanks

Trimtrom

Public Class frmManual
Dim dcManual As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Nwind 97.mdb")
Dim daManual As OleDb.OleDbDataAdapter
Dim dsManual As New DataSet
Dim strSQL As String

Private Sub frmManual_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

strSQL = "select * from employees"
Try
daManual = New OleDb.OleDbDataAdapter(strSQL, dcManual)
daManual.Fill(dsManual, "Employees")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Me.dgManual.DataSource = dsManual.Tables("Employees")
End Sub

Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
Dim ans As String
If (dsManual.HasChanges) Then
ans = MsgBox("Changes have occurred!" & vbCrLf & "Do you wish to update?", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "Updates")
If ans = vbYes Then
Try
dsManual.AcceptChanges()
daManual.Update(dsManual.Tables("Employees"))
Catch exManual As Exception
MsgBox(exManual.Message)
Throw exManual
Finally
End Try
MsgBox("Table updated!", MsgBoxStyle.Critical, "Employees")
End If
End If
End Sub
End Class
 
The problem is with these 2 lines:

dsManual.AcceptChanges()
daManual.Update(dsManual.Tables("Employees"))

The AcceptChanges call does exactly that - it accepts all changes to the dataset and then removes the indicators that show there were changes. Thus, when the Update call is made in the next line, the dataset shows that there are no changes (they were accepted in the previous line) and so no update is performed, because the dataadapter "thinks" there are no records to update. Simply reverse the order of these 2 lines and it should work.

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top