473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datarelation

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. Imports System.Data
  3. Imports System.Data.OleDb
  4. Public Class Form1
  5.     Inherits System.Windows.Forms.Form
  6.  
  7. #Region " Windows Form Designer generated code "
  8.  
  9.     Private Const APP_TITLE As String = "[ datarelation ] "
  10.     'Thats the whole code . 
  11.     Friend WithEvents MyBMBase As BindingManagerBase 'Pointer For Records
  12.     'Path to database
  13.     Dim dbpath As String = Application.StartupPath & "\Notes.mdb"
  14.     'Connection obj to database
  15.     Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath & ";Jet OLEDB:Database Password=")
  16.     'Dataset that holds data in disconnected mode
  17.     'Dim ds As New DataSet
  18.     'Two commands for two tables (tab1 and tab2)
  19.     Dim cmd1 As OleDbCommand
  20.     Dim cmd2 As OleDbCommand
  21.     'Two datapaters to fill the dataset from two tables
  22.     Dim adp1 As OleDbDataAdapter
  23.     Dim adp2 As OleDbDataAdapter
  24.     'This handles the relationship between the two columns 
  25.     Dim datarelation As datarelation
  26.     'Dim Foreign_Key_Constraint As ForeignKeyConstraint
  27.     Dim dc1 As DataColumn
  28.     Dim dc2 As DataColumn
  29.  
  30.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  31.  
  32.         'Open the Connetion 
  33.         conn.Open()
  34.         'It's not important but gives your code more better way to 
  35.         'compare strings between tables
  36.         ds.CaseSensitive = True
  37.  
  38.         'First command for first table
  39.         cmd1 = New OleDbCommand
  40.         cmd1.Connection = conn
  41.         cmd1.CommandText = "SELECT * FROM Name  order by firstname"
  42.  
  43.  
  44.         'Second command for Second table
  45.         cmd2 = New OleDbCommand
  46.         cmd2.Connection = conn
  47.         cmd2.CommandText = "SELECT * FROM info"
  48.  
  49.         'Now , we will fill the first table and add it to the dataset
  50.         adp1 = New OleDbDataAdapter
  51.         adp1.SelectCommand = cmd1
  52.         adp1.TableMappings.Add("Table", "Name")
  53.         adp1.Fill(ds)
  54.  
  55.  
  56.         'As we did in the previous step , here for the Second table
  57.         adp2 = New OleDbDataAdapter
  58.         adp2.SelectCommand = cmd2
  59.         adp2.TableMappings.Add("Table", "info")
  60.         Try
  61.             adp2.Fill(DS)
  62.         Catch ex As Exception
  63.             MsgBox(ex.Message)
  64.         End Try
  65.         ' close conn
  66.         If conn.State.Open = ConnectionState.Open Then
  67.             conn.Close()
  68.         End If
  69.         'Add DataRelation
  70.         dc1 = DS.Tables("Name").Columns("ID")
  71.         dc2 = DS.Tables("info").Columns("ID")
  72.         '''Here we combined two datacolumns to the relations obj 
  73.         Try
  74.             datarelation = New DataRelation("NameAndInfo", dc1, dc2, True)
  75.             ' Foreign_Key_Constraint = datarelation.ChildKeyConstraint
  76.             ' Foreign_Key_Constraint.UpdateRule = Rule.Cascade
  77.             ' Foreign_Key_Constraint.DeleteRule = Rule.Cascade
  78.             ' Foreign_Key_Constraint.AcceptRejectRule = AcceptRejectRule.Cascade
  79.             DS.Relations.Add(datarelation)
  80.             ' binding all text
  81.         txtIDName.DataBindings.Add(New Binding("Text", DS, "Name.ID"))
  82.         txtFirstName.DataBindings.Add(New Binding("Text", DS, "Name.FirstName"))
  83.         txtLastName.DataBindings.Add(New Binding("Text", DS, "Name.LastName"))
  84.         txtIDInfo.DataBindings.Add(New Binding("Text", DS, "Name.NameAndInfo.ID"))
  85.         txtPhone.DataBindings.Add(New Binding("Text", DS, "Name.NameAndInfo.Phone"))
  86.             txtMobile.DataBindings.Add(New Binding("Text", DS, "Name.NameAndInfo.Mobile"))
  87.         Catch ex As Exception
  88.             MsgBox(ex.Message)
  89.         End Try
  90.         'Simple one , bind the dataset after all operation to the 
  91.         'Datagrid
  92.         DataGrid1.DataSource = DS.DefaultViewManager
  93.         'Show the first table in the grid because it's the primary table
  94.         DataGrid1.DataMember = "Name"
  95.         Try
  96.             MyBMBase = Me.BindingContext(DS, "Name")
  97.             ShowPosition()
  98.         Catch ex As Exception
  99.             MsgBox(ex.Message)
  100.         End Try
  101.     End Sub
  102.     ' Display the position.
  103.     Private Sub MyBMBase_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBMBase.PositionChanged
  104.         ShowPosition()
  105.     End Sub
  106.     Private Sub ShowPosition()
  107.         Me.Text = APP_TITLE & "  ===  " & MyBMBase.Position + 1 & "/" & MyBMBase.Count
  108.     End Sub
  109.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
  110.         Try
  111.             MyBMBase.EndCurrentEdit()
  112.             If DS.HasChanges = True Then
  113.                 Select Case MsgBox("Save changes?", MsgBoxStyle.YesNoCancel, "Save Changes?")
  114.                     Case MsgBoxResult.Yes
  115.                         ' Save the changes.
  116.                         SaveChanges()
  117.                     Case MsgBoxResult.Cancel
  118.                         ' Cancel the exit.
  119.                         e.Cancel = True
  120.                     Case MsgBoxResult.No
  121.                         ' Do nothing. Just exit.
  122.                 End Select
  123.             End If
  124.         Catch ex As Exception
  125.             MessageBox.Show(ex.Message, " Save ", MessageBoxButtons.OK, _
  126.             MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, _
  127.             MessageBoxOptions.RtlReading)
  128.         End Try
  129.     End Sub
  130.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  131.         If MyBMBase.Position < MyBMBase.Count - 1 Then
  132.             Try
  133.                 MyBMBase.Position += 1
  134.             Catch ex As Exception
  135.                 MsgBox(ex.Message)
  136.             End Try
  137.         End If
  138.     End Sub
  139.  
  140.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  141.         If MyBMBase.Position > 0 Then
  142.             Try
  143.                 MyBMBase.Position -= 1
  144.             Catch ex As Exception
  145.                 MsgBox(ex.Message)
  146.             End Try
  147.         End If
  148.     End Sub
  149.  
  150.     Private Sub ButFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButFirst.Click
  151.         MyBMBase.Position = 0
  152.     End Sub
  153.  
  154.     Private Sub ButLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButLast.Click
  155.         MyBMBase.Position = MyBMBase.Count - 1
  156.     End Sub
  157.     ' Save changes to the database.
  158.     Private Sub SaveChanges()
  159.         Dim cb1 As New OleDbCommandBuilder(adp1)
  160.         Dim ds_changes As DataSet
  161.         Dim ds_subset As DataSet
  162.         Try
  163.  
  164.  
  165.             ' Finish the current edit.
  166.             MyBMBase.EndCurrentEdit()
  167.  
  168.             ' See if there are any changes to save.
  169.             If DS.HasChanges Then
  170.                 ' Get a DataSet holding the changes.
  171.                 ds_changes = DS.GetChanges()
  172.  
  173.                 ' Save the changes grouped by type.
  174.                 '@
  175.                 ' This is supposed to be more efficient than making 
  176.                 ' the changes in a bunch. See ADO.NET p 316-7.
  177.                 '@
  178.                 ds_subset = ds_changes.GetChanges(DataRowState.Modified)
  179.                 If (Not (ds_subset) Is Nothing) Then adp1.Update(ds_subset)
  180.  
  181.                 ds_subset = ds_changes.GetChanges(DataRowState.Added)
  182.                 If (Not (ds_subset) Is Nothing) Then adp1.Update(ds_subset)
  183.  
  184.                 ds_subset = ds_changes.GetChanges(DataRowState.Deleted)
  185.                 If (Not (ds_subset) Is Nothing) Then adp1.Update(ds_subset)
  186.  
  187.                 ' Mark the modified records as not modified.
  188.                 DS.AcceptChanges()
  189.             End If
  190.         Catch ex As Exception
  191.             MsgBox(ex.Message)
  192.         End Try
  193.     End Sub
  194.  
  195.     Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAddNew.Click
  196.         ' Finish the current edit.
  197.         MyBMBase.EndCurrentEdit()
  198.  
  199.         ' Add the new record. This automatically
  200.         ' causes the CurrencyManager to reposition
  201.         ' so it displays the new position.
  202.         MyBMBase.AddNew()
  203.  
  204.         ' Set the focus to the Last Name field.
  205.         txtFirstName.Focus()
  206.         txtIDName.Text = "*"
  207.         txtIDInfo.Text = "*"
  208.     End Sub
  209.  
  210.     Private Sub ButSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButSave.Click
  211.         If MyBMBase.Count = 0 Then Exit Sub
  212.         SaveChanges()
  213.         MsgBox("Ok", MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, "Changes Saved")
  214.     End Sub
  215.  
  216.     Private Sub ButCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButCancel.Click
  217.         ' Finish the current edit.
  218.         MyBMBase.EndCurrentEdit()
  219.  
  220.         ' See if there are any changes to cancel.
  221.         If DS.HasChanges Then
  222.             ' Make the user confirm.
  223.             If MsgBox("Discard changes?", MsgBoxStyle.YesNo, "Discard changes?") = MsgBoxResult.No Then Exit Sub
  224.  
  225.             ' Cancel the changes.
  226.             DS.RejectChanges()
  227.         End If
  228.     End Sub
  229.  
  230.     Private Sub ButDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButDelete.Click
  231.         If MyBMBase.Count = 0 Then Exit Sub
  232.         ' Finish the current edit.
  233.         MyBMBase.EndCurrentEdit()
  234.  
  235.         ' Make the user confirm.
  236.         If MsgBox("Delete this record?", MsgBoxStyle.YesNo, "Delete Record?") = MsgBoxResult.No Then Exit Sub
  237.  
  238.         ' Delete the record.
  239.         MyBMBase.RemoveAt(MyBMBase.Position)
  240.  
  241.         ' Display the new position.
  242.         ShowPosition()
  243.     End Sub
  244. End Class
  245.  
  246.  
  247.  
Thank you!
Aug 18 '05 #1
1 4740
afzalhaque
1 New Member
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?
Apr 25 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1566
by: serge calderara | last post by:
Dear all, I have a dataset which contains 2 DataTable The idea is to bind a text box control with one table field and then diaplaye the associated content of the second table based on the first table entry. For that I have been guide to use data relation bettween those two table. The following code illustrate what I have done :
0
1130
by: Stefan | last post by:
I need help with the following problem. I created a datarelation DataRelation oRelation = new DataRelation("plz_relation",odsAdressenDS.v_plz.plz_idColumn, odsAdressenDS.v_poststelle.plz_idColumn); odsAdressenDS.Relations.Add(oRelation); I know want to add this relation to two comboboxes
0
1731
by: Fox | last post by:
Is it possible to get back the DataRelation contect from a dataSet? { DataRelation myDataRelation; DataColumn parentColumn; DataColumn childColumn; parentColumn = myDataSet.Tables.Columns; childColumn = myDataSet.Tables.Columns; myDataRelation = new DataRelation("parent2Child", parentColumn, childColumn);...
1
3582
by: Johann Blake | last post by:
I have a dataset that contains a parent table and a child table. A DataRelation exists between the two. I was under the impression from reading the VS docs that when I filled the parent table, the child table would be automatically filled with the child records. When I fill the parent, I limit the table to only a single record, so that all the...
0
1348
by: mazda_88 | last post by:
I have a number of tables inside a dataset. I have a repeater that I need to bind to. The data that is needed resides in two tables in the dataset. Here is the code that I'm using: Dim rel_Bridesmaid As DataRelation rel_Bridesmaid = New DataRelation("BridemaidOrder", _...
0
1923
by: frank | last post by:
I have several tables that have a one to one relationship. I have DataRelation objects to define these relationships. I would like to bind the DataRelation to both a gridview and a formview so that I can use different fields from different tables in a single gridview and formview. In the examples I have seen the gridview and formview are...
2
11459
by: Rich | last post by:
Hello, Following an example at http://www.vb-tips.com/dbpages.aspx?IA=DG (by Cor Lightert and Ken Tucker) on binding a dataRelation to a Datagridview for sqlClient, I was able to view rows in datagridview2 that corresponded to a selected row in datagridview1. Great article/example.
6
1973
by: Aryan | last post by:
Hi, I want to use DataRelation object for setting up relationship between two different datatable, but i want to use multiple columns to set up relation between two tables. For example. DataRelation dRelationOrgInd = new DataRelation("OrgInd", ds.Tables.Columns, ds.Tables.Columns, true); dRelationOrgInd.Nested = true;...
2
1911
by: Tony Johansson | last post by:
Hello! I can't understand what is this table DataRelation used for. I mean as long as you have this DataTable I can't see any point in using this DataRelation. //Tony
0
7542
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7736
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7982
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7500
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7827
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5110
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3514
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1079
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.