Primary key insert in a Detailsview control.

Posted on 2009-02-21
Last Modified: 2012-08-13
I'm using a Detailsveiew control for adding a new contact to tbl_contacts. The control is bound to a class in the middle tier...clscontacts.
I need to take a new primary key from a table of lastkeysused and use it for the new contact being entered in the Detailsview. I can retrieve the new key...but I cannot get it into the detailsview row so it can be included in the INSERT.
Can it be done? or am I taking the wrong approach?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            'Get the next key for this insert

            Dim mylist As List(Of clslastkey)

            Dim myclslastkey As clslastkey

            mylist = LastKeyDAL.GetLastKey() 'get the list...just 1 record in this case

            myclslastkey = mylist(0) ' get the first item in the list

            lblnextcontactID.Text = myclslastkey.nextcontactID 'get the next key        

        End If

    End Sub

Open in new window

Question by:chrisgreaves
    LVL 11

    Assisted Solution

    Chris, normally the database would be setup to generate the new number itself (autonumber type) in Access, or identity property set to true in SQL Server.

    Trying to guess what the next value in the database would be would be problematic if
    a) the last record entered has been deleted
    b) two users are concurrently in the process of creating a user.

    Maybe a better approach is to generate the record, and then query the database to find out what the value of what last record inserted is.

    LVL 3

    Accepted Solution

    Hi Mildurait,
    Thanks for the comments, you make good points regarding the deletion of records and the simultaneous insertion of new records by multiple users...I need to have that discussion with my instructor at the next class.
    BUT, for now, I am "instructor bound" to do it this way.
    I woke up fresh this morning and figured out these 2 lines, which take care of my immediate needs:
                Dim row1 As DetailsViewRow = DetailsView1.Rows(0)
                row1.Cells(1).Text = myclslastkey.nextcontactID

    Thanks again, and hopefully, you are nowhere near the bushfires.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
    Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now