-
Oct 11th, 2019, 07:26 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Help me understand primary key in a DataTable
vb.net Code:
Dim TaxIDs As New DataTable("TaxIDs")
TaxIDs.Columns.Add("JDX", GetType(Integer))
TaxIDs.Columns.Add("TaxID", GetType(String))
TaxIDs.PrimaryKey = New DataColumn() {TaxIDs.Columns("TaxID")}
Is this the correct way to make my TaxID column the primary key? It seems strange to me because I don't understand. And the MS documentation didn't help me. I'm just not bright enough to understand them.
If so, how does one use the .contains method? I was thinking I have a string value and I would do something like this to avoid duplicates:
vb.net Code:
If Not TaxIDs.PrimaryKey.Contains(strTaxID) Then
Dim Row As DataRow = TaxIDs.NewRow
Row("JDX") = JDX
Row("TaxID") = strTaxID
TaxIDs.Rows.Add(Row)
End If
But of course that's not right.
Can someone straighten up my code and explain how this works?
-
Oct 11th, 2019, 07:51 PM
#2
Re: Help me understand primary key in a DataTable
I don't use VB nor do I use datatables, but from the documentation it looks like you're setting up your primary key correctly.
To search the table based on the primary key, you should be able to do:
vb.net Code:
If TaxIDs.Rows.Find(strTaxID) = null Then Dim Row As DataRow = TaxIDs.NewRow Row("JDX") = JDX Row("TaxID") = strTaxID TaxIDs.Rows.Add(Row) End If
Documentation for this is here: https://docs.microsoft.com/en-us/dot...System_Object_
-
Oct 11th, 2019, 08:34 PM
#3
Re: Help me understand primary key in a DataTable
DataTables work pretty much the same way as database tables. In a database table, you add columns to describe the data (data type, size, etc) and you add rows to contain the data. A DataTable contains DataColumns and DataRows for the same purpose. In a database table, you set one or more columns to be the primary key so that values in a single column or combinations of values in multiple columns will uniquely identify the records in the table. A DataTable has a PrimaryKey property for the same purpose. In a database table, you can generally specify a surrogate key by configuring a single PK column to auto-generate sequential numerical values. A DataColumn has AutoIncrement, AutoIncrementSeed and AutoIncrementStep properties for the same purpose.
-
Oct 11th, 2019, 08:37 PM
#4
Re: Help me understand primary key in a DataTable
Originally Posted by cory_jackson
If so, how does one use the .contains method? I was thinking I have a string value and I would do something like this to avoid duplicates:
vb.net Code:
If Not TaxIDs.PrimaryKey.Contains(strTaxID) Then Dim Row As DataRow = TaxIDs.NewRow Row("JDX") = JDX Row("TaxID") = strTaxID TaxIDs.Rows.Add(Row) End If
But of course that's not right.
Can someone straighten up my code and explain how this works?
The PrimaryKey property is an array of DataColumns that specifies how rows are identified. It's not the values in the rows. The rows contain all the values so it's the rows - stored in the Rows collection - that need to be examined to find a particular PK value.
-
Oct 11th, 2019, 08:38 PM
#5
Re: Help me understand primary key in a DataTable
Originally Posted by kfcSmitty
I don't use VB nor do I use datatables, but from the documentation it looks like you're setting up your primary key correctly.
To search the table based on the primary key, you should be able to do:
vb.net Code:
If TaxIDs.Rows.Find(strTaxID) = null Then Dim Row As DataRow = TaxIDs.NewRow Row("JDX") = JDX Row("TaxID") = strTaxID TaxIDs.Rows.Add(Row) End If
Documentation for this is here: https://docs.microsoft.com/en-us/dot...System_Object_
Small correction for VB:
vb.net Code:
If TaxIDs.Rows.Find(strTaxID) Is Nothing Then
-
Oct 12th, 2019, 11:00 AM
#6
Re: Help me understand primary key in a DataTable
This isn't truly database related, so I think you may get more answers in .NET (if you WANT more answers, that is, since you have some pretty decent ones, already).
My usual boring signature: Nothing
-
Oct 14th, 2019, 05:41 PM
#7
Thread Starter
Frenzied Member
Re: Help me understand primary key in a DataTable
Sorry Shaggy. I thought DataTables were something one worked with when interacting with databases so I thought this would be a better group. Normally I post things in a more general category and I get my knuckles rapped and my post moved to the more specific forum. I was just trying to do what I thought was right and not get admonished for posting in the general category.
-
Oct 14th, 2019, 05:46 PM
#8
Re: Help me understand primary key in a DataTable
Originally Posted by cory_jackson
Sorry Shaggy. I thought DataTables were something one worked with when interacting with databases so I thought this would be a better group. Normally I post things in a more general category and I get my knuckles rapped and my post moved to the more specific forum. I was just trying to do what I thought was right and not get admonished for posting in the general category.
DataTables and databases often go hand in hand but they are not the same thing. It's possible to use a DataTable without a database being involved. Your question relates completely to DataTables and has nothing at all to do with databases, even if there is a database in use in your app. If the question related to primary keys in a database and how they interacted with primary keys in a DataTable then that would be an appropriate question for the Database Development forum.
-
Oct 14th, 2019, 05:47 PM
#9
Thread Starter
Frenzied Member
Re: Help me understand primary key in a DataTable
vb.net Code:
If Not TaxIDs.PrimaryKey.Contains(strTaxID) Then
Thanks all. I still don't understand why I can't do what MS had. I must have something wrong. So I can see that the PrimaryKey would be like an index. If I set it to point to TaxID, I should be able to see if that index/column contains the string value I want with the code above but I get "Value of type 'String' cannot be converted to 'DataColumn'." from Intellisense. I don't understand. It want's me to supply a column of strings? But I don't want to check for multiple strings, I only want to see if one is contained. It just seems like one should be able o use the index to see if that column of string contains a string. The concept of being able to find a column of data in a column of data boggles my mind. Obviously way over my head.
I'll just ditch the idea of making it a primary key and go back to doing a simple find. But thanks again for all your help.
-
Oct 14th, 2019, 05:48 PM
#10
Thread Starter
Frenzied Member
Re: Help me understand primary key in a DataTable
I'm sorry John. I didn't mean to offend anyone by posting i there. I will try to get it moved ASAP and mark it resolved.
-
Oct 14th, 2019, 05:55 PM
#11
Thread Starter
Frenzied Member
Re: Help me understand primary key in a DataTable
I can't find any instructions or methods to move my inappropriate post. I've sent a message to the sysop asking for help. If any of you can point me to instructions, I'd appreciate it. Or you can report me. I read the FAQs subjects and nothing in there.
-
Oct 14th, 2019, 05:57 PM
#12
Thread Starter
Frenzied Member
Re: Help me understand primary key in a DataTable
Originally Posted by jmcilhinney
The PrimaryKey property is an array of DataColumns that specifies how rows are identified. It's not the values in the rows. The rows contain all the values so it's the rows - stored in the Rows collection - that need to be examined to find a particular PK value.
I think I understand John. I'll give that a try and do some experiments. Thank you.
-
Oct 14th, 2019, 06:07 PM
#13
Thread Starter
Frenzied Member
Re: [RESOLVED] Help me understand primary key in a DataTable
vb.net Code:
If Not TaxIDs.Rows.Contains(strTaxID) Then
Seems to work perfectly. You're right John, need to look in the rows for data. Thanks again.
-
Oct 14th, 2019, 06:18 PM
#14
Re: [RESOLVED] Help me understand primary key in a DataTable
Originally Posted by cory_jackson
I'm sorry John. I didn't mean to offend anyone by posting i there. I will try to get it moved ASAP and mark it resolved.
You didn't offend anyone. It's not a case of offence. It's simply that things should be done a particular way and they weren't so that was corrected and the reason explained. If someone tells you that you did the wrong thing, it doesn't mean that they think you're a bad person. It is probably just an explanation of why what you did was wrong and what the correct alternative was so that you can avoid making the same mistake again, for everyone's sake. It's simply a matter of keeping the site as ordered as possible.
Originally Posted by cory_jackson
I can't find any instructions or methods to move my inappropriate post. I've sent a message to the sysop asking for help. If any of you can point me to instructions, I'd appreciate it. Or you can report me. I read the FAQs subjects and nothing in there.
You don't move it yourself. It's already been moved. When I read your question, I thought that it was more appropriate for VB.NET than Database Development so I clicked the Report Post icon under post #1 and said as much. That message goes out to the mods and, when Shaggy read it, he obviously agreed and moved the thread. If you ever do need a thread moved, you can use the same mechanism.
-
Oct 14th, 2019, 06:23 PM
#15
Thread Starter
Frenzied Member
Re: [RESOLVED] Help me understand primary key in a DataTable
I don't think I was a bad person and I don't think any of you thought I was a bad person.
Thanks for reporting it so it could be moved.
I found the button to report! I didn't realize the triangle meant "report post" I even searched the web page for the word "report" but it wasn't found. I see it now when one hovers over it, no visible text that uses the word. Thank you for the edification.
-
Oct 14th, 2019, 07:58 PM
#16
Re: [RESOLVED] Help me understand primary key in a DataTable
Originally Posted by cory_jackson
I don't think I was a bad person and I don't think any of you thought I was a bad person.
I could have thought that. You don't know.
-
Oct 15th, 2019, 10:25 AM
#17
Re: [RESOLVED] Help me understand primary key in a DataTable
There's often a bit of a question with threads. You wouldn't ask a question if you didn't want a good answer (well, hopefully, anyways), so the goal of any thread should be to get the best possible response. Some questions are clearly suited to one place or another. Others are not so clearly suited to one or another, at which point one has to consider where the best answer will come from. In this case, database development was not unreasonable, and you clearly got good answers there. I just felt that you would get better (or at least more) answers to that particular question in this forum.
Also, when we move a thread, we generally leave a link in the original place so that people can follow it to the new location. This has the additional benefit that the thread is visible from both places for a time.
A couple other notes: Reporting can be used to ask for a thread to be moved/deleted, or even closed. We may or may not do it, or may take a long time getting around to it, but that's what the report is there for. Also, nobody else can move a thread. At one point, it was possible for the thread starter to delete an entire thread by deleting the first post in the thread. These days, I don't believe that's still possible. Haven't tried it, though. You might need over 2000 posts to unlock that capability.
My usual boring signature: Nothing
-
Oct 18th, 2019, 08:37 PM
#18
Thread Starter
Frenzied Member
Re: [RESOLVED] Help me understand primary key in a DataTable
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
|