Results 1 to 3 of 3

Thread: ? about getting Primary Key for a batch insert *RESOLVED*

  1. #1

    Thread Starter
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Question ? about getting Primary Key for a batch insert *RESOLVED*

    ok guys and girls...i finally got my question figured out that i asked a few days ago, but now i have another one. if i add a record to the one table that has a PKey that autoincrements...is there a way to get the PKey to that record that i just added so that i can add records to another table where PKey is a FKey in that table....i hope that makes sense...i've posted the code as well so that you can see what is happening up to the point of where i want to perform this action that i'm asking about....sorry about the newbie questions....just so use to vb6 and old ado. also i delete some of the code that doesn't have anything to do with what i'm asking.


    VB Code:
    1. Private Sub SaveNewShipLog()
    2.         Dim LibInfoAdp As OleDbDataAdapter 'For Library Information Table access
    3.         Dim LibProjAdp As OleDbDataAdapter 'For Project Table access
    4.         Dim ShipLogAdp As OleDbDataAdapter 'For Shipping Log Table access
    5.         Dim ShipListAdp As OleDbDataAdapter 'For Shipping Log Lists Table access
    6.         Dim adoDs As DataSet
    7.  
    8.         Try
    9.             'Setup the adapters
    10.             LibInfoAdp = New OleDbDataAdapter(Me.BulidShippingLogSQL("library information"), adoCnn)
    11.             LibProjAdp = New OleDbDataAdapter(Me.BulidShippingLogSQL("lib_proj_tbl"), adoCnn)
    12.             ShipLogAdp = New OleDbDataAdapter(Me.BulidShippingLogSQL("ship_log_tbl"), adoCnn)
    13.             ShipListAdp = New OleDbDataAdapter(Me.BulidShippingLogSQL("shp_log_lstn_tbl"), adoCnn)
    14.  
    15.             'Make sure the adapters that the primary key.
    16.             LibInfoAdp.MissingSchemaAction = MissingSchemaAction.AddWithKey
    17.             LibProjAdp.MissingSchemaAction = MissingSchemaAction.AddWithKey
    18.             ShipLogAdp.MissingSchemaAction = MissingSchemaAction.AddWithKey
    19.             ShipListAdp.MissingSchemaAction = MissingSchemaAction.AddWithKey
    20.  
    21.  
    22.             'Fill the dataset.
    23.             adoDs = New DataSet
    24.             LibInfoAdp.Fill(adoDs, "[Library Information]")
    25.             LibProjAdp.Fill(adoDs, "lib_proj_tbl")
    26.             ShipLogAdp.FillSchema(adoDs, SchemaType.Mapped, "ship_log_tbl")
    27.             ShipListAdp.FillSchema(adoDs, SchemaType.Mapped, "shp_log_lstn_tbl")
    28.  
    29.             Try
    30.                 'Test for library information change
    31.                 Me.LibraryInfoUpdateTest(adoDs)
    32.             Catch ex As Exception
    33.                 MsgBox(ex.Message)
    34.             End Try
    35.  
    36.             Try
    37.                 'Test for Project budget change.
    38.                 If adoDs.Tables("lib_proj_tbl").Rows(0)("proj_budget") <> CDbl(Me.txtProjectAmt.Text.Replace(",", "")) Then
    39.                     adoCmd = New OleDbCommand("UPDATE lib_proj_tbl " & _
    40.                                               "SET proj_budget = " & _
    41.                                               CDbl(Me.txtProjectAmt.Text.Replace(",", "")) & _
    42.                                               " WHERE LIB_ID = " & lngLIB_ID & _
    43.                                               " AND PROJ_ID = " & lngPROJ_ID, adoCnn)
    44.  
    45.                     adoCmd.ExecuteNonQuery()
    46.                     adoCmd.Dispose()
    47.                     adoCmd = Nothing
    48.                 End If
    49.             Catch ex As Exception
    50.                 MsgBox(ex.Message)
    51.             End Try
    52.  
    53.             Try
    54.                 'Add Shipping Log and List
    55.                 AddNewShippingLog(ShipLogAdp, adoDs)
    56.             Catch ex As Exception
    57.                 MsgBox(ex.Message)
    58.             End Try
    59.  
    60.             'Set up a command builder
    61.             Dim cBldr = New OleDbCommandBuilder(ShipLogAdp)
    62.  
    63.             ShipLogAdp.Update(adoDs, "ship_log_tbl")
    64.             cBldr = Nothing
    65.         Catch ex As Exception
    66.                 MsgBox(ex.Message)
    67.             End Try
    68.  
    69.             adoDs.Dispose()
    70.             LibInfoAdp.Dispose()
    71.             LibProjAdp.Dispose()
    72.             adoDs = Nothing
    73.             LibInfoAdp = Nothing
    74.             LibProjAdp = Nothing
    75.     End Sub
    76.  
    77.     Private Sub AddNewShippingLog(ByVal adp As OleDbDataAdapter, ByVal ds As DataSet)
    78.         Dim dRow As DataRow = ds.Tables("ship_log_tbl").NewRow
    79.  
    80.         dRow("PROJ_ID") = lngPROJ_ID
    81.         dRow("SHP_MTH_ID") = CInt(Me.cboShipMethod.Text.Substring(0, Me.cboShipMethod.Text.IndexOf(" ", 0)))
    82.         If Me.optListTypeNSO.Checked Then
    83.             dRow("NSO_ID") = CInt(Me.cboListTypeNSO.Text.Substring(0, Me.cboListTypeNSO.Text.IndexOf(" ", 0)))
    84.         End If
    85.         If Me.optListTypeODC.Checked Then
    86.             dRow("ODC_ID") = CInt(Me.cboListTypeODC.Text.Substring(0, Me.cboListTypeODC.Text.IndexOf(" ", 0)))
    87.         End If
    88.         If Me.optListTypeReg.Checked Then
    89.             dRow("REG_COLLCT_BLDR_LST_ID") = CInt(Me.cboListTypeReg.Text.Substring(0, Me.cboListTypeReg.Text.IndexOf(" ", 0)))
    90.         End If
    91.         If Me.optListTypeCompleat.Checked Then
    92.             dRow("COMP_BKLST_ID") = 1
    93.         End If
    94.         If Me.optListTypeBID.Checked Then
    95.             dRow("BID_CB_LST_ID") = 1
    96.         End If
    97.         dRow("ship_date") = CDate(Me.DateTimePicker1.Value.ToShortDateString)
    98.         dRow("attetion_of") = Me.txtAttention.Text.Trim()
    99.         dRow("cd_cs_person") = Me.txtCDCSPerson.Text.Trim()
    100.         dRow("lst_pricing") = IIf(CInt(Me.cboListPrice.Text.Substring(0, Me.cboListPrice.Text.IndexOf(" ", 0))) = 1, True, False)
    101.         dRow("spec_notes") = Me.txtSpecialNotes.Text.Trim()
    102.         dRow("fmt_paper") = IIf(Me.chkFormatPaper.Checked, True, False)
    103.         dRow("fmt_disk") = IIf(Me.chkFormatDisk.Checked, True, False)
    104.         dRow("fmt_FTP") = IIf(Me.chkFormatFTP.Checked, True, False)
    105.         dRow("fmt_BIBZ") = IIf(Me.chkFormatBIBZ.Checked, True, False)
    106.         dRow("fmt_email") = IIf(Me.chkFormatEmail.Checked, True, False)
    107.         dRow("fmt_spd_sht") = IIf(Me.chkFormatSpreadSh.Checked, True, False)
    108.  
    109.         ds.Tables("ship_log_tbl").Rows.Add(dRow)
    110.         dRow = Nothing
    111.     End Sub
    112.  
    113.     Private Function BulidShippingLogSQL(ByVal DB_Table_Name As String) As String
    114.         Select Case DB_Table_Name.ToLower
    115.             Case "library information"
    116.                 Return "SELECT " & _
    117.                             "ID, " & _
    118.                             "[Library Name], " & _
    119.                             "Address1, " & _
    120.                             "Address2, " & _
    121.                             "City, " & _
    122.                             "State, " & _
    123.                             "Zip, " & _
    124.                             "[Library Type] " & _
    125.                        "FROM " & _
    126.                             "[Library Information] " & _
    127.                        "WHERE " & _
    128.                             "ID = " & lngLIB_ID
    129.             Case "lib_proj_tbl"
    130.                 Return "SELECT * " & _
    131.                        "FROM " & _
    132.                             "lib_proj_tbl " & _
    133.                        "WHERE " & _
    134.                             "PROJ_ID = " & lngPROJ_ID & " AND LIB_ID = " & lngLIB_ID
    135.             Case "ship_log_tbl"
    136.                 Return "SELECT * FROM ship_log_tbl WHERE PROJ_ID = " & lngPROJ_ID
    137.             Case "shp_log_lstn_tbl"
    138.                 Return "SELECT * FROM shp_log_lstn_tbl"
    139.             Case Else
    140.  
    141.         End Select
    142.     End Function
    Last edited by vbdotnetboy; Jul 2nd, 2004 at 10:09 AM.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  2. #2
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    If you where using a sql server procedure you could return the new PK but seeing as your using something else the only way I can think of achieving this is to, after insertion select the last record inserted by dooing something like

    "Select top 1 from myTbl order desc"

  3. #3

    Thread Starter
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Thumbs up

    Ok this may not be the best way...and if anyone else has a suggestion i'm all ears however here's what i did in order to get the PKey after adding the new record so that i can do the batch insert



    MODIFIED CODE:
    VB Code:
    1. Private Sub SaveNewShipLog()
    2.         Dim LibInfoAdp As OleDbDataAdapter 'For Library Information Table access
    3.         Dim LibProjAdp As OleDbDataAdapter 'For Project Table access
    4.         Dim ShipLogAdp As OleDbDataAdapter 'For Shipping Log Table access
    5.         Dim ShipListAdp As OleDbDataAdapter 'For Shipping Log Lists Table access
    6.         Dim adoDs As DataSet
    7.  
    8.         Try
    9.             'Setup the adapters
    10.             LibInfoAdp = New OleDbDataAdapter(Me.BulidShippingLogSQL("library information"), adoCnn)
    11.             LibProjAdp = New OleDbDataAdapter(Me.BulidShippingLogSQL("lib_proj_tbl"), adoCnn)
    12.             ShipLogAdp = New OleDbDataAdapter(Me.BulidShippingLogSQL("ship_log_tbl"), adoCnn)
    13.             ShipListAdp = New OleDbDataAdapter(Me.BulidShippingLogSQL("shp_log_lstn_tbl"), adoCnn)
    14.  
    15.             'Make sure the adapters that the primary key.
    16.             LibInfoAdp.MissingSchemaAction = MissingSchemaAction.AddWithKey
    17.             LibProjAdp.MissingSchemaAction = MissingSchemaAction.AddWithKey
    18.             ShipLogAdp.MissingSchemaAction = MissingSchemaAction.AddWithKey
    19.             ShipListAdp.MissingSchemaAction = MissingSchemaAction.AddWithKey
    20.  
    21.  
    22.             'Fill the dataset.
    23.             adoDs = New DataSet
    24.             LibInfoAdp.Fill(adoDs, "[Library Information]")
    25.             LibProjAdp.Fill(adoDs, "lib_proj_tbl")
    26.             ShipLogAdp.Fill(adoDs, "ship_log_tbl")
    27.             ShipListAdp.Fill(adoDs, "shp_log_lstn_tbl")
    28.  
    29.             Try
    30.                 'Test for library information change
    31.                 Me.LibraryInfoUpdateTest(adoDs)
    32.             Catch ex As Exception
    33.                 MsgBox(ex.Message)
    34.             End Try
    35.  
    36.             Try
    37.                 'Test for Project budget change.
    38.                 If adoDs.Tables("lib_proj_tbl").Rows(0)("proj_budget") <> CDbl(Me.txtProjectAmt.Text.Replace(",", "")) Then
    39.                     adoCmd = New OleDbCommand("UPDATE lib_proj_tbl " & _
    40.                                               "SET proj_budget = " & _
    41.                                               CDbl(Me.txtProjectAmt.Text.Replace(",", "")) & _
    42.                                               " WHERE LIB_ID = " & lngLIB_ID & _
    43.                                               " AND PROJ_ID = " & lngPROJ_ID, adoCnn)
    44.  
    45.                     adoCmd.ExecuteNonQuery()
    46.                     adoCmd.Dispose()
    47.                     adoCmd = Nothing
    48.                 End If
    49.             Catch ex As Exception
    50.                 MsgBox(ex.Message)
    51.             End Try
    52.  
    53.  
    54.             Try
    55.                 'Add Shipping Log and List
    56.                 AddNewShippingLog(ShipLogAdp, adoDs)
    57.             Catch ex As Exception
    58.                 MsgBox(ex.Message)
    59.             End Try
    60.  
    61.             'Set up a command builder
    62.             Dim cBldr = New OleDbCommandBuilder(ShipLogAdp)
    63.  
    64.             ShipLogAdp.Update(adoDs, "ship_log_tbl")
    65.            
    66.             'ADDED NEW CODE HERE
    67.             'Refresh the Dataset, making sure it has PKey for the new record.
    68.             ShipLogAdp.Fill(adoDs, "ship_log_tbl")
    69.             'Testing to check if correct PKey, will be replaced with routine to do batch insert.
    70.             MsgBox(adoDs.Tables("ship_log_tbl").Rows(adoDs.Tables("ship_log_tbl").Rows.Count - 1).Item("SLT_ID"))
    71.  
    72.             cBldr = Nothing
    73.         Catch ex As Exception
    74.                 MsgBox(ex.Message)
    75.             End Try
    76.  
    77.             adoDs.Dispose()
    78.             LibInfoAdp.Dispose()
    79.             LibProjAdp.Dispose()
    80.             adoDs = Nothing
    81.             LibInfoAdp = Nothing
    82.             LibProjAdp = Nothing
    83.     End Sub


    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

Posting Permissions

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



Click Here to Expand Forum to Full Width