So(u)rcerer
May 28th, 2008, 07:43 AM
Hello again everyone.
In my project, I'm using a Bindingnavigator to be able to navigate through all the records in an Access table. This is what I did:
Imports System.Data
Imports System.Data.OleDb
Public Class frmPlay
Private CDsBS As BindingSource
Private CDsDS As DataSet
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Create a BindingSource for all the BindingNavigators to use
CDsBS = New BindingSource()
CDsBS.AllowNew = True
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CDsDS = RetrieveCDs()
If CDsDS IsNot Nothing Then
' Associate the DataSet with the BindingSource.
Me.CDsBS.DataMember = "CDs"
Me.CDsBS.DataSource = CDsDS
' Associate the BindingNavigators with the BindingSource
Me.bnPlayCD.BindingSource = Me.CDsBS
' Bind the form controls
Me.txtPCDArtist.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.CDsBS, "CDArtist", True))
Me.txtPCDTitle.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.CDsBS, "CDTitle", True))
Me.cboPCDType.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.CDsBS, "CDType", True))
Me.cboPCDListType.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.CDsBS, "CDListType", True))
End If
End Sub
Public Function RetrieveCDs() As DataSet
Try
' Retrieve CD data from database into a DataSet
' Build a connnection string to the database
Dim CDsCSB As New OleDbConnectionStringBuilder
CDsCSB.DataSource = ".\Play.mdb"
CDsCSB.Provider = "Microsoft.Jet.OLEDB.4.0"
' Prepare a DataSet to receive the CD data
Dim CDsDS As New DataSet("CDs")
' Open connection to the Play database
Using CDsCon As New OleDbConnection(CDsCSB.ConnectionString)
CDsCon.Open()
' Retrieve CD data
Dim command As New OleDbCommand( _
"SELECT * FROM [CDs]", CDsCon)
Using drCDs As OleDbDataReader = command.ExecuteReader()
CDsDS.Load( _
drCDs, _
LoadOption.OverwriteChanges, _
New String() {"CDs"})
End Using
' Close the connection to the database
CDsCon.Close()
End Using
Return CDsDS
Catch err As OleDbException
MessageBox.Show(err.Message, "OLE DB Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return Nothing
End Try
End Function
Private Sub btnPCDUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPCDUpdate.Click
CDsBS.EndEdit()
CDsDS.AcceptChanges()
End Sub
End Class
Currently, it does show all the fields and values present in the table.
The problem is, after I have clicked the Add New button on the BindingNavigator, and entered the info, and clicked Update, it does not save the record into the table.
What must I do, just to be able to complete the adding record process?
Can anyone help?
In my project, I'm using a Bindingnavigator to be able to navigate through all the records in an Access table. This is what I did:
Imports System.Data
Imports System.Data.OleDb
Public Class frmPlay
Private CDsBS As BindingSource
Private CDsDS As DataSet
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Create a BindingSource for all the BindingNavigators to use
CDsBS = New BindingSource()
CDsBS.AllowNew = True
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CDsDS = RetrieveCDs()
If CDsDS IsNot Nothing Then
' Associate the DataSet with the BindingSource.
Me.CDsBS.DataMember = "CDs"
Me.CDsBS.DataSource = CDsDS
' Associate the BindingNavigators with the BindingSource
Me.bnPlayCD.BindingSource = Me.CDsBS
' Bind the form controls
Me.txtPCDArtist.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.CDsBS, "CDArtist", True))
Me.txtPCDTitle.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.CDsBS, "CDTitle", True))
Me.cboPCDType.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.CDsBS, "CDType", True))
Me.cboPCDListType.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.CDsBS, "CDListType", True))
End If
End Sub
Public Function RetrieveCDs() As DataSet
Try
' Retrieve CD data from database into a DataSet
' Build a connnection string to the database
Dim CDsCSB As New OleDbConnectionStringBuilder
CDsCSB.DataSource = ".\Play.mdb"
CDsCSB.Provider = "Microsoft.Jet.OLEDB.4.0"
' Prepare a DataSet to receive the CD data
Dim CDsDS As New DataSet("CDs")
' Open connection to the Play database
Using CDsCon As New OleDbConnection(CDsCSB.ConnectionString)
CDsCon.Open()
' Retrieve CD data
Dim command As New OleDbCommand( _
"SELECT * FROM [CDs]", CDsCon)
Using drCDs As OleDbDataReader = command.ExecuteReader()
CDsDS.Load( _
drCDs, _
LoadOption.OverwriteChanges, _
New String() {"CDs"})
End Using
' Close the connection to the database
CDsCon.Close()
End Using
Return CDsDS
Catch err As OleDbException
MessageBox.Show(err.Message, "OLE DB Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return Nothing
End Try
End Function
Private Sub btnPCDUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPCDUpdate.Click
CDsBS.EndEdit()
CDsDS.AcceptChanges()
End Sub
End Class
Currently, it does show all the fields and values present in the table.
The problem is, after I have clicked the Add New button on the BindingNavigator, and entered the info, and clicked Update, it does not save the record into the table.
What must I do, just to be able to complete the adding record process?
Can anyone help?