VB.Net - Trying to Set DataSet DataRelations

Good Day Experts!

I am trying to setup a DataSet with two DataTables and a DataRelations.  Unfortunately I am getting an error that I cannot seem to rid the error.

Here is my code:

                            Dim tblTargetPriKey(1) As DataColumn
                            Dim tblTarget2PriKey(1) As DataColumn
                            tblTargetPriKey(1) = tblTarget.Columns("col2")
                            tblTarget2PriKey(1) = tblTarget2.Columns("Pro Number")
                            tblTarget.PrimaryKey = tblTargetPriKey
                            tblTarget2.PrimaryKey = tblTarget2PriKey
                            TargetDataSet.Tables.Add(tblTarget)
                            TargetDataSet.Tables.Add(tblTarget2)
                            Dim TargetParent As DataColumn
                            Dim Target2Child As DataColumn
                            TargetParent = TargetDataSet.Tables("tblTarget").Columns("col2")
                            Target2Child = TargetDataSet.Tables("tblTarget2").Columns("Pro Number")
                            Dim tblTargetRelation As DataRelation
                            tblTargetRelation = New DataRelation("Relations", TargetParent, Target2Child)
                            TargetDataSet.Relations.Add(tblTargetRelation)

Error is on line:

TargetParent = TargetDataSet.Tables("tblTarget").Columns("col2")

Error text:

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."

I have searched the net and cannot figure out what I need to do to repair. Do you have any ideas?

Thanks,
jimbo99999
Jimbo99999Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
can you try:
TargetParent = TargetDataSet.Tables(0).Columns("col2")

Open in new window

0
Jimbo99999Author Commented:
It worked!

Thanks...I wonder the reasoning?

jimbo99999
0
Éric MoreauSenior .Net ConsultantCommented:
You just didn't set the TableName of each of your tables so you need to reference them by their position
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jimbo99999Author Commented:
Excellent...I understand now.  I appreciate the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.