Results 1 to 3 of 3

Thread: more than one column in a datarelation

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    more than one column in a datarelation

    Hello,

    All the samples I got only has 1 column that relates 2 tables.

    Dim MyDataRelation As DataRelation
    MyDataRelation = New DataRelation("BrUserDept", dtBranchUser.Columns("userid"), dtBranchDept.Columns("userid"))
    dsUserDept.Relations.Add(MyDataRelation)

    How will I relate 2 tables using more than one column?

    Thanks in advance.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    Got it!

    VB Code:
    1. Sub CreateDataSet()
    2.  
    3.         Dim MyConnection As New SqlConnection()
    4.         MyConnection.ConnectionString = ConfigurationSettings.AppSettings("OpalConnect")
    5.         Try
    6.  
    7.             Dim strSQL As String = "SELECT userid, branchcode, def FROM BranchUser"
    8.             Dim scmd As New SqlCommand(strSQL, MyConnection)
    9.             sda = New SqlDataAdapter(scmd)
    10.             Dim scb As New SqlCommandBuilder(sda)
    11.             scb.GetUpdateCommand()
    12.  
    13.             ' Create a new DataSet and fill its first DataTable.
    14.             dsUserDept = New DataSet()
    15.             sda.Fill(dsUserDept, "BranchUser")
    16.  
    17.             ' Reset the CommandText to get the User dept
    18.             scmd.CommandText = "select userid, branchcode, deptcode, def from BranchDept"
    19.  
    20.             ' Fill the second table in the DataSet.
    21.             sda.Fill(dsUserDept, "BranchDept")
    22.  
    23.             ' Set variables for the DataTables for use later.
    24.             dtBranchUser = dsUserDept.Tables(0)
    25.             dtBranchDept = dsUserDept.Tables(1)
    26.  
    27.             ' Set up DataViews for the DataGrids
    28.             dvBranchUser = dtBranchUser.DefaultView
    29.             dvBranchDept = dtBranchDept.DefaultView
    30.  
    31.             Dim MyDataRelation As DataRelation
    32.             Dim ParentCols() As DataColumn
    33.             Dim ChildCols() As DataColumn
    34.  
    35.             ParentCols(0) = dtBranchUser.Columns("userid")
    36.             ParentCols(1) = dtBranchUser.Columns("branchcode")
    37.  
    38.             ChildCols(0) = dtBranchDept.Columns("userid")
    39.             ChildCols(1) = dtBranchDept.Columns("branchcode")
    40.  
    41.             MyDataRelation = New DataRelation("BrUserDept", ParentCols, ChildCols)
    42.  
    43.             dsUserDept.Relations.Add(MyDataRelation)
    44.    
    45. End Sub

    But I got an error message when I run it:

    Object reference not set to an instance of an object

    What could be wrong?

    Thanks in advance.

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Its because of the way you declared the array of columns. You can not say for example:

    Dim X() as Integer
    X(0)=10

    I think that's because 'Dim x() as Integer' does not allocate memory for x(). You have to redim.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width