|
-
Jan 25th, 2004, 11:40 PM
#1
Thread Starter
Addicted Member
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.
-
Jan 26th, 2004, 12:50 AM
#2
Thread Starter
Addicted Member
Got it!
VB Code:
Sub CreateDataSet()
Dim MyConnection As New SqlConnection()
MyConnection.ConnectionString = ConfigurationSettings.AppSettings("OpalConnect")
Try
Dim strSQL As String = "SELECT userid, branchcode, def FROM BranchUser"
Dim scmd As New SqlCommand(strSQL, MyConnection)
sda = New SqlDataAdapter(scmd)
Dim scb As New SqlCommandBuilder(sda)
scb.GetUpdateCommand()
' Create a new DataSet and fill its first DataTable.
dsUserDept = New DataSet()
sda.Fill(dsUserDept, "BranchUser")
' Reset the CommandText to get the User dept
scmd.CommandText = "select userid, branchcode, deptcode, def from BranchDept"
' Fill the second table in the DataSet.
sda.Fill(dsUserDept, "BranchDept")
' Set variables for the DataTables for use later.
dtBranchUser = dsUserDept.Tables(0)
dtBranchDept = dsUserDept.Tables(1)
' Set up DataViews for the DataGrids
dvBranchUser = dtBranchUser.DefaultView
dvBranchDept = dtBranchDept.DefaultView
Dim MyDataRelation As DataRelation
Dim ParentCols() As DataColumn
Dim ChildCols() As DataColumn
ParentCols(0) = dtBranchUser.Columns("userid")
ParentCols(1) = dtBranchUser.Columns("branchcode")
ChildCols(0) = dtBranchDept.Columns("userid")
ChildCols(1) = dtBranchDept.Columns("branchcode")
MyDataRelation = New DataRelation("BrUserDept", ParentCols, ChildCols)
dsUserDept.Relations.Add(MyDataRelation)
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.
-
Jan 26th, 2004, 02:12 AM
#3
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|