Hi Everyone,
Can you tell me how do I set up create Insert, Update and Delete commands for your a DataAdapter?
I'm using a DataGridView to allow the user to view, change and delete data rows and would like to be able to save any changes made in the grid back to the database.
The application is part of one I'm developing for our local school to write reports to report cards for the children.
Here is the code I used to set it up:
' Setup the Connection string.
'-----------------------------
Dim strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Emad's Section\Development\Visual Studio\Projects\ISGL School Application\ISGL School Application\iap1.mdb"
' Declare string to hold SQL query.
'----------------------------------
Dim strQuery As String = _
"SELECT attendance.AttendanceID, " & _
"class.ClassName, " & _
"Student.StudentId, LTrim(Student.FirstName) & ' ' & LTrim(Student.LastName) AS StudentName, " & _
"Int((Date()-dob)/365.25) AS StudentAge, " & _
"attendance.DateOfClass, " & _
"attendance.Absent " & _
"FROM Class " & _
"INNER JOIN (Attendance INNER JOIN Student ON Attendance.StudentID = Student.StudentId) " & _
"ON Class.ClassId = Attendance.ClassId"
' Declare a database connection.
'-------------------------------
Dim objConnection As OleDbConnection
' Declare a command using the SQL query that will be run.
'--------------------------------------------------------
Dim objCommand As OleDbCommand
' Declare a DataAdapter.
'-----------------------
Dim objDataAdapter As OleDbDataAdapter
' Declare a DataSet.
'-------------------
Dim objDataSet As DataSet
' Declare a DataView. We will filter on this.
'--------------------------------------------
Dim objDataView As DataView
Private Sub frmAttendance_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
' Initialize the Connection object.
'----------------------------------
objConnection = New OleDbConnection(strConnectionString)
' Initialize the Command object with the SQL query.
'--------------------------------------------------
objCommand = New OleDbCommand(strQuery, objConnection)
objConnection.Open()
' Initialize the DataSet.
'------------------------
objDataSet = New DataSet()
' Initialize the DataAdapter.
'----------------------------
objDataAdapter = New OleDbDataAdapter()
' Initialize the DataView.
'-------------------------
objDataView = New DataView
' Set DataAdapter command properties.
'------------------------------------
objDataAdapter.SelectCommand = objCommand
' Populate the Dataset.
'----------------------
objDataAdapter.Fill(objDataSet, "Attendance Data")
objDataView.Table = objDataSet.Tables("Attendance Data")
If (objDataSet.Tables("Attendance Data").Rows.Count = 0) Then
MessageBox.Show("There is currently no attendance data in the database.")
Else
DataGridViewAttendance.DataSource = objDataView
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
objConnection.Close()
End Try
End Sub
I'm rather new to VB 2008 and all help will be appreciated.
I can also post the project if needed.
Thanks.
Truly,
Emad