PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Help me understand primary key in a DataTable-VBForums
Results 1 to 18 of 18

Thread: [RESOLVED] Help me understand primary key in a DataTable

  1. #1

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    979

    Resolved [RESOLVED] Help me understand primary key in a DataTable

    vb.net Code:
    1. Dim TaxIDs As New DataTable("TaxIDs")
    2. TaxIDs.Columns.Add("JDX", GetType(Integer))
    3. TaxIDs.Columns.Add("TaxID", GetType(String))
    4. 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:
    1. If Not TaxIDs.PrimaryKey.Contains(strTaxID) Then
    2.     Dim Row As DataRow = TaxIDs.NewRow
    3.     Row("JDX") = JDX
    4.     Row("TaxID") = strTaxID
    5.     TaxIDs.Rows.Add(Row)
    6. End If
    But of course that's not right.
    Can someone straighten up my code and explain how this works?

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,177

    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:
    1. If TaxIDs.Rows.Find(strTaxID) = null Then
    2.     Dim Row As DataRow = TaxIDs.NewRow
    3.     Row("JDX") = JDX
    4.     Row("TaxID") = strTaxID
    5.     TaxIDs.Rows.Add(Row)
    6. End If

    Documentation for this is here: https://docs.microsoft.com/en-us/dot...System_Object_

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,185

    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.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,185

    Re: Help me understand primary key in a DataTable

    Quote Originally Posted by cory_jackson View Post
    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:
    1. If Not TaxIDs.PrimaryKey.Contains(strTaxID) Then
    2.     Dim Row As DataRow = TaxIDs.NewRow
    3.     Row("JDX") = JDX
    4.     Row("TaxID") = strTaxID
    5.     TaxIDs.Rows.Add(Row)
    6. 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.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,185

    Re: Help me understand primary key in a DataTable

    Quote Originally Posted by kfcSmitty View Post
    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:
    1. If TaxIDs.Rows.Find(strTaxID) = null Then
    2.     Dim Row As DataRow = TaxIDs.NewRow
    3.     Row("JDX") = JDX
    4.     Row("TaxID") = strTaxID
    5.     TaxIDs.Rows.Add(Row)
    6. End If

    Documentation for this is here: https://docs.microsoft.com/en-us/dot...System_Object_
    Small correction for VB:
    vb.net Code:
    1. If TaxIDs.Rows.Find(strTaxID) Is Nothing Then

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,724

    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

  7. #7

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    979

    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.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,185

    Re: Help me understand primary key in a DataTable

    Quote Originally Posted by cory_jackson View Post
    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.

  9. #9

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    979

    Re: Help me understand primary key in a DataTable

    vb.net Code:
    1. 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.

  10. #10

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    979

    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.

  11. #11

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    979

    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.

  12. #12

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    979

    Re: Help me understand primary key in a DataTable

    Quote Originally Posted by jmcilhinney View Post
    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.

  13. #13

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    979

    Re: [RESOLVED] Help me understand primary key in a DataTable

    vb.net Code:
    1. 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.

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,185

    Re: [RESOLVED] Help me understand primary key in a DataTable

    Quote Originally Posted by cory_jackson View Post
    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.
    Quote Originally Posted by cory_jackson View Post
    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.

  15. #15

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    979

    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.

  16. #16
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,185

    Re: [RESOLVED] Help me understand primary key in a DataTable

    Quote Originally Posted by cory_jackson View Post
    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.

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,724

    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

  18. #18

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    979

    Re: [RESOLVED] Help me understand primary key in a DataTable

    Thanks guys.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width