Hi,
I have a datagridview and the datasource is a database query. This query takes data from multiple tables within the database.
When I run the project the data loads to the datagridview fine. However, the system needs to allow the user to update data shown in the datagrid and save it.
I've been messing with this for ages now and it is still not saving correctly. Here is the code behind the save button:
When I run the project and hit the save button, the "update successful" msgbox will actually appear but changes are not actually saved.
I'm not even sure I need all that code regarding the InsertCommand because it's just updating that needs to be done, no new rows can be created.
Another thing I did was add some code behind the dataset;
Again, I'm not sure if this is needed, I just saw it when I was googling this problem and said I'd give it a go.
Your help would be greatly appreciated as this functionality is crucial to my project and if I can just get it working here, I'll be able to apply it to other areas of the project.
Thanks,
Laura
I have a datagridview and the datasource is a database query. This query takes data from multiple tables within the database.
When I run the project the data loads to the datagridview fine. However, the system needs to allow the user to update data shown in the datagrid and save it.
I've been messing with this for ages now and it is still not saving correctly. Here is the code behind the save button:
VB.NET:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim strUpdate As String = "Update tblAttendance set Reasonforabsence = @Reason For Absence"
Dim strInsert As String = "Insert Into TodaysAbsentees values (@Student, @Class Name, @AttendanceDate, @Reason For Absence)"
Dim sqlUpdate, sqlInsert As OleDb.OleDbCommand
sqlInsert = New OleDb.OleDbCommand(strInsert, objConnection)
sqlUpdate = New OleDb.OleDbCommand(strUpdate, objConnection)
sqlUpdate.Parameters.Add("@Reason For Absence", Data.OleDb.OleDbType.Char, 255, "Reasonforabsence")
sqlInsert.Parameters.Add("@Student", Data.OleDb.OleDbType.Char, 255, "Student")
sqlInsert.Parameters.Add("@Class Name", Data.OleDb.OleDbType.Char, 255, "ClassName")
sqlInsert.Parameters.Add("@AttendanceDate", Data.OleDb.OleDbType.Char, 255, "AttendanceDate")
sqlInsert.Parameters.Add("@Reason For Absence", Data.OleDb.OleDbType.Char, 255, "Reasonforabsence")
TodaysAbsentessTableAdapter = New OleDb.OleDbDataAdapter()
TodaysAbsentessTableAdapter.UpdateCommand = sqlUpdate
TodaysAbsentessTableAdapter.InsertCommand = sqlInsert
If Me.DataSet2.HasChanges Then
Try
DataSet2.TodaysAbsentees.GetChanges()
TodaysAbsentessTableAdapter.Update(DataSet2, "tblAttendance")
TodaysAbsentessTableAdapter.AcceptChangesDuringUpdate = True
DataSet2.AcceptChanges()
MsgBox("Update Successful")
Catch ex As Exception
MsgBox("Update Failed")
End Try
End If
I'm not even sure I need all that code regarding the InsertCommand because it's just updating that needs to be done, no new rows can be created.
Another thing I did was add some code behind the dataset;
VB.NET:
Partial Class DataSet2
End Class
Namespace DataSet2TableAdapters
Partial Public Class TodaysAbsenteesTableAdapter
Public Sub AssignConnection(ByVal conn As OleDb.OleDbConnection, _
Optional ByVal trans As OleDb.OleDbTransaction = Nothing)
Me.Connection = conn
If trans IsNot Nothing Then
Me.Adapter.InsertCommand.Transaction = trans
Me.Adapter.UpdateCommand.Transaction = trans
End If
Me.Adapter.AcceptChangesDuringUpdate = True
Me.Adapter.ContinueUpdateOnError = False
trans.Commit()
End Sub
End Class
End Namespace
Your help would be greatly appreciated as this functionality is crucial to my project and if I can just get it working here, I'll be able to apply it to other areas of the project.
Thanks,
Laura