isPrimaryKey, Assigning Primary Key in VB.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newbtemple
    New Member
    • Feb 2008
    • 31

    isPrimaryKey, Assigning Primary Key in VB.net

    New to programming and got a question. I'm trying to add a primary key in vb.net to a table I made in SQL. The table in SQL does not have a primary key. I pull that table into my program and concatenate two columns and assign a series of numbers from a for loop to rows with nulls, to make a third column that is unique enough to be a primary key.

    This is where i'm getting stuck. I'm trying to figure out how to assign a primary key in VB after i've pulled the data in from SQL.

    Dim pkColumn(0) As DataColumn


    pkColumn(0) = DS.Tables("CBOE OI").Columns("C ompleteSymbol")
    DS.Tables("CBOE OI").PrimaryKey () = pkColumn


    adaptor.UpdateC ommand = cmdBuilder.GetU pdateCommand()
    adaptor.Update( DS.Tables("CBOE OI"))

    If I run this I get

    "{"Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information."},

    which indicates to me that the primary key wasn't set.

    I've tried to set up isParentKey without much luck. I've done:

    'test to determine if parent key was established
    'datarelation = New DataRelation("C S", DS.Tables("CBOE OI").Columns("C ompleteSymbol") , DS.Tables("CBOE OI").Columns("C ompleteSymbol") , False)
    'DS.Relations.A dd(datarelation )
    'datarelation = DS.Relations("C BOEOI")
    'uconstraint = datarelation.Pa rentKeyConstrai nt

    'If uconstraint.IsP rimaryKey Then
    ' MsgBox("Complet eSymbol is the primary key")
    'Else
    ' MsgBox("doh")

    'End If

    But I can't figure out how to make it work without multiple tables. I get a both tables are the same error which is what I want since I only use one column for a key.

    Any suggestions for how to resolve this would be greatly appreciated.
    Last edited by newbtemple; Feb 28 '08, 05:01 PM. Reason: cleaning up my title
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Looks to me like you get that error because you are trying to have it automatically create an update query for a DataTable that no longer matches the schema of your database table (which also does not have any keys).
    Create you own UPDATE command manually, or add a spare column to your SQL table to store the progmatically-computed "primary key"?

    Is there a reason why you didn't make a multi-column key in your SQL table?

    Comment

    • newbtemple
      New Member
      • Feb 2008
      • 31

      #3
      Originally posted by Plater
      Looks to me like you get that error because you are trying to have it automatically create an update query for a DataTable that no longer matches the schema of your database table (which also does not have any keys).
      Create you own UPDATE command manually, or add a spare column to your SQL table to store the progmatically-computed "primary key"?

      Is there a reason why you didn't make a multi-column key in your SQL table?


      right in vb and in sql I don't have a primary key. That is what I am trying to figure out how to do. I've wasted so much time trying to google and msdn this problem that I think i'm just gonna give up and make a random value for a primary key in SQL.

      My current situation is this:

      1. I get 5 txt file
      2. I copy that txt file into excel and then parse it
      3. Pull data into SQL. There are 5 sheets. All 5 are combined into one one table.
      4. VB program pulls some of the dolumns from SQL. There are rows that are identicle. These are usually titles or somesuch and have no value as data. They repeat a few thousand times and have to be ignored or deleted.
      5. Take that data and isolate certain fields that show up in rows (and basically nothing else on that row) and assign them repeatedly in a new column.

      Ill take taht updated information and send it back to teh SQL table. Create a new table with other data and Query the two.

      the schema is different becuase I don't pull all the data from SQL, only the columns I really need. I'l change that and see if it works.

      thanks!

      Comment

      • newbtemple
        New Member
        • Feb 2008
        • 31

        #4
        Originally posted by newbtemple
        right in vb and in sql I don't have a primary key. That is what I am trying to figure out how to do. I've wasted so much time trying to google and msdn this problem that I think i'm just gonna give up and make a random value for a primary key in SQL.

        My current situation is this:

        1. I get 5 txt file
        2. I copy that txt file into excel and then parse it
        3. Pull data into SQL. There are 5 sheets. All 5 are combined into one one table.
        4. VB program pulls some of the dolumns from SQL. There are rows that are identicle. These are usually titles or somesuch and have no value as data. They repeat a few thousand times and have to be ignored or deleted.
        5. Take that data and isolate certain fields that show up in rows (and basically nothing else on that row) and assign them repeatedly in a new column.

        Ill take taht updated information and send it back to teh SQL table. Create a new table with other data and Query the two.

        the schema is different becuase I don't pull all the data from SQL, only the columns I really need. I'l change that and see if it works.

        thanks!
        UPDATE:

        I made both tables with identicle columns and created a random value in excel. When I pull the data into SQL, I assign it as primary key. It works like a charm but it's an added step for the end user I wish wasn't there. Oh wells, i'll get this program done and then goback and try to figure out how to make it more efficient.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I don't think you need the number to be there, just the column.
          The column can be empty and you fill it in progmatically.
          I do this frequently.
          I'll pull a spare column in my query and populate it in my code and use it as a temp-value column tpye thing.

          Comment

          Working...