Datarelation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yaser
    New Member
    • Aug 2005
    • 1

    Datarelation

    Hello:
    I need save my update but i can't
    :confused: :confused: :confused:
    look my code :

    Code:
    Imports System.Data
    Imports System.Data.OleDb
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Private Const APP_TITLE As String = "[ datarelation ] "
        'Thats the whole code . 
        Friend WithEvents MyBMBase As BindingManagerBase 'Pointer For Records
        'Path to database
        Dim dbpath As String = Application.StartupPath & "\Notes.mdb"
        'Connection obj to database
        Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath & ";Jet OLEDB:Database Password=")
        'Dataset that holds data in disconnected mode
        'Dim ds As New DataSet
        'Two commands for two tables (tab1 and tab2)
        Dim cmd1 As OleDbCommand
        Dim cmd2 As OleDbCommand
        'Two datapaters to fill the dataset from two tables
        Dim adp1 As OleDbDataAdapter
        Dim adp2 As OleDbDataAdapter
        'This handles the relationship between the two columns 
        Dim datarelation As datarelation
        'Dim Foreign_Key_Constraint As ForeignKeyConstraint
        Dim dc1 As DataColumn
        Dim dc2 As DataColumn
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'Open the Connetion 
            conn.Open()
            'It's not important but gives your code more better way to 
            'compare strings between tables
            ds.CaseSensitive = True
    
            'First command for first table
            cmd1 = New OleDbCommand
            cmd1.Connection = conn
            cmd1.CommandText = "SELECT * FROM Name  order by firstname"
    
    
            'Second command for Second table
            cmd2 = New OleDbCommand
            cmd2.Connection = conn
            cmd2.CommandText = "SELECT * FROM info"
    
            'Now , we will fill the first table and add it to the dataset
            adp1 = New OleDbDataAdapter
            adp1.SelectCommand = cmd1
            adp1.TableMappings.Add("Table", "Name")
            adp1.Fill(ds)
    
    
            'As we did in the previous step , here for the Second table
            adp2 = New OleDbDataAdapter
            adp2.SelectCommand = cmd2
            adp2.TableMappings.Add("Table", "info")
            Try
                adp2.Fill(DS)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            ' close conn
            If conn.State.Open = ConnectionState.Open Then
                conn.Close()
            End If
            'Add DataRelation
            dc1 = DS.Tables("Name").Columns("ID")
            dc2 = DS.Tables("info").Columns("ID")
            '''Here we combined two datacolumns to the relations obj 
            Try
                datarelation = New DataRelation("NameAndInfo", dc1, dc2, True)
                ' Foreign_Key_Constraint = datarelation.ChildKeyConstraint
                ' Foreign_Key_Constraint.UpdateRule = Rule.Cascade
                ' Foreign_Key_Constraint.DeleteRule = Rule.Cascade
                ' Foreign_Key_Constraint.AcceptRejectRule = AcceptRejectRule.Cascade
                DS.Relations.Add(datarelation)
                ' binding all text
            txtIDName.DataBindings.Add(New Binding("Text", DS, "Name.ID"))
            txtFirstName.DataBindings.Add(New Binding("Text", DS, "Name.FirstName"))
            txtLastName.DataBindings.Add(New Binding("Text", DS, "Name.LastName"))
            txtIDInfo.DataBindings.Add(New Binding("Text", DS, "Name.NameAndInfo.ID"))
            txtPhone.DataBindings.Add(New Binding("Text", DS, "Name.NameAndInfo.Phone"))
                txtMobile.DataBindings.Add(New Binding("Text", DS, "Name.NameAndInfo.Mobile"))
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            'Simple one , bind the dataset after all operation to the 
            'Datagrid
            DataGrid1.DataSource = DS.DefaultViewManager
            'Show the first table in the grid because it's the primary table
            DataGrid1.DataMember = "Name"
            Try
                MyBMBase = Me.BindingContext(DS, "Name")
                ShowPosition()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        ' Display the position.
        Private Sub MyBMBase_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBMBase.PositionChanged
            ShowPosition()
        End Sub
        Private Sub ShowPosition()
            Me.Text = APP_TITLE & "  ===  " & MyBMBase.Position + 1 & "/" & MyBMBase.Count
        End Sub
        Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            Try
                MyBMBase.EndCurrentEdit()
                If DS.HasChanges = True Then
                    Select Case MsgBox("Save changes?", MsgBoxStyle.YesNoCancel, "Save Changes?")
                        Case MsgBoxResult.Yes
                            ' Save the changes.
                            SaveChanges()
                        Case MsgBoxResult.Cancel
                            ' Cancel the exit.
                            e.Cancel = True
                        Case MsgBoxResult.No
                            ' Do nothing. Just exit.
                    End Select
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, " Save ", MessageBoxButtons.OK, _
                MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, _
                MessageBoxOptions.RtlReading)
            End Try
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If MyBMBase.Position < MyBMBase.Count - 1 Then
                Try
                    MyBMBase.Position += 1
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If MyBMBase.Position > 0 Then
                Try
                    MyBMBase.Position -= 1
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End Sub
    
        Private Sub ButFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButFirst.Click
            MyBMBase.Position = 0
        End Sub
    
        Private Sub ButLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButLast.Click
            MyBMBase.Position = MyBMBase.Count - 1
        End Sub
        ' Save changes to the database.
        Private Sub SaveChanges()
            Dim cb1 As New OleDbCommandBuilder(adp1)
            Dim ds_changes As DataSet
            Dim ds_subset As DataSet
            Try
    
    
                ' Finish the current edit.
                MyBMBase.EndCurrentEdit()
    
                ' See if there are any changes to save.
                If DS.HasChanges Then
                    ' Get a DataSet holding the changes.
                    ds_changes = DS.GetChanges()
    
                    ' Save the changes grouped by type.
                    '@
                    ' This is supposed to be more efficient than making 
                    ' the changes in a bunch. See ADO.NET p 316-7.
                    '@
                    ds_subset = ds_changes.GetChanges(DataRowState.Modified)
                    If (Not (ds_subset) Is Nothing) Then adp1.Update(ds_subset)
    
                    ds_subset = ds_changes.GetChanges(DataRowState.Added)
                    If (Not (ds_subset) Is Nothing) Then adp1.Update(ds_subset)
    
                    ds_subset = ds_changes.GetChanges(DataRowState.Deleted)
                    If (Not (ds_subset) Is Nothing) Then adp1.Update(ds_subset)
    
                    ' Mark the modified records as not modified.
                    DS.AcceptChanges()
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    
        Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAddNew.Click
            ' Finish the current edit.
            MyBMBase.EndCurrentEdit()
    
            ' Add the new record. This automatically
            ' causes the CurrencyManager to reposition
            ' so it displays the new position.
            MyBMBase.AddNew()
    
            ' Set the focus to the Last Name field.
            txtFirstName.Focus()
            txtIDName.Text = "*"
            txtIDInfo.Text = "*"
        End Sub
    
        Private Sub ButSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButSave.Click
            If MyBMBase.Count = 0 Then Exit Sub
            SaveChanges()
            MsgBox("Ok", MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, "Changes Saved")
        End Sub
    
        Private Sub ButCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButCancel.Click
            ' Finish the current edit.
            MyBMBase.EndCurrentEdit()
    
            ' See if there are any changes to cancel.
            If DS.HasChanges Then
                ' Make the user confirm.
                If MsgBox("Discard changes?", MsgBoxStyle.YesNo, "Discard changes?") = MsgBoxResult.No Then Exit Sub
    
                ' Cancel the changes.
                DS.RejectChanges()
            End If
        End Sub
    
        Private Sub ButDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButDelete.Click
            If MyBMBase.Count = 0 Then Exit Sub
            ' Finish the current edit.
            MyBMBase.EndCurrentEdit()
    
            ' Make the user confirm.
            If MsgBox("Delete this record?", MsgBoxStyle.YesNo, "Delete Record?") = MsgBoxResult.No Then Exit Sub
    
            ' Delete the record.
            MyBMBase.RemoveAt(MyBMBase.Position)
    
            ' Display the new position.
            ShowPosition()
        End Sub
    End Class
    Thank you!
    Last edited by yaser; Aug 18 '05, 08:05 AM.
  • afzalhaque
    New Member
    • Apr 2008
    • 1

    #2
    hey can any help me out!!!!!!! i want to know da conxept of modules in VB or what are modules in VB and are procedures and modules the same thing?

    Comment

    Working...