PrimaryKey not working for a DataTable?

I have defined a DataTable in my windows app. I've also created a PrimaryKey for this table, however, when I go to delete a row in the DataTable, it throws an exception saying "Table doesn't have a Primary Key". I've defined the DataTable in a Module that executes when the program starts, so it's clearly defined. I've also attached the code where it tries to delete a row. I'm wondering if there is anyway to reinitialize the table other than "DataTable = Nothing" or "DataTable.Rows.Clear"? I never set the DataTable to Nothing so I don't understand why it would throw this exception.

The below segment of code is defined in a Module.

            'TEST_DETAILS_Load Table
            dbTEST_DETAILS_Load = New DataTable

            Dim tdl_Key(2) As DataColumn
            Dim dtlpk0 As New DataColumn
            Dim dtlpk1 As New DataColumn
            Dim dtlpk2 As New DataColumn

            dtlpk0.ColumnName = "channel_ID"
            dtlpk0.DataType = System.Type.GetType("System.Int16")

            dtlpk1.ColumnName = "testNO"
            dtlpk1.DataType = System.Type.GetType("System.Int16")

            dtlpk2.ColumnName = "loadingPoint"
            dtlpk2.DataType = System.Type.GetType("System.Int16")

            dbTEST_DETAILS_Load.PrimaryKey = tdl_Key

            dbTEST_DETAILS_Load.Columns.Add("loadValue", System.Type.GetType("System.Decimal"))
            dbTEST_DETAILS_Load.Columns.Add("conversionValue", System.Type.GetType("System.String"))
            dbTEST_DETAILS_Load.Columns.Add("output", System.Type.GetType("System.String"))
            dbTEST_DETAILS_Load.Columns.Add("nonLinearity", System.Type.GetType("System.String"))
            dbTEST_DETAILS_Load.Columns.Add("hysteresis", System.Type.GetType("System.String"))
            dbTEST_DETAILS_Load.Columns.Add("forceError", System.Type.GetType("System.String"))
            dbTEST_DETAILS_Load.Columns.Add("loadLink_ID", System.Type.GetType("System.Int16"))

Open in new window

The below code resides within a Form Module and is just a piece of code within a Function. The DataTable assigns rows to a DataGridView. For this particular example, the DataTable contains 42 rows and the DataGridView shows 7 rows at a time.

                    For z As Integer = 0 To uc.dgvTension.Rows.Count - 1
                        Dim strKey2(2) As Object

                        strKey2(0) = uc.Tag
                        strKey2(1) = uc.testNO
                        strKey2(2) = z + 1

                        Dim row2 As DataRow = dbTEST_DETAILS_Load.Rows.Find(strKey2)     -------> Exception thrown here

                        If Not IsNothing(row2) Then
                        End If

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Jacques Bourgeois (James Burger)PresidentCommented:
You declare tdl_Key(2), but you do not fill the array. You thus end up assigning an empty array to your PrimaryKey property.

Assuming that the 3 columns that you defined are the components of your primary key, you need to do something like the following:

dbTEST_DETAILS_Load.PrimaryKey = New DataColumn() {dtlpk0, dtlpk1, dtlpk2}

If you have never seen that type of New, it is a way of creating an array and filling it at the same time.

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
BlakeMcKennaAuthor Commented:
Thanks James...that worked!
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.