how can I retrieve a newly added autoincremented key index?

how can I retrieve a newly added autoincremented key index
I created an insertquery in my .xsd designer.  It worked perfectly.


Dim AT As String = Me.AuthorTypeTextBox.Text

Dim Active As Boolean = Me.ActiveCheckBox.Checked

Me.AuthorTypeTableAdapter.InsertQuery(AT, Active)

Unfortunately I can't find a way to retrieve the autoincrementing key index.  No event of the tableadapter or bindingsource allows me to grab it.

dennist


Answer this question

how can I retrieve a newly added autoincremented key index?

  • ChrisDurbin

    Just followup with a query for the record with the highest index value.
  • rwindsley

    thanks r, 

    but I'm using an access database.  When I tried

    Dim cmd As System.Data.OleDb.OleDbCommand = Me.AuthorTypeTableAdapter.SelectCommand.Connection.CreateCommand

    I get the message, selectcommand is not a member of authortypetableadapter.

    dennist685

  • kielbasa

    Thanks, r

    I looked through books online for sql server and ms access help,  But I couldn't find the syntax for the query you suggested.  In any case, from the code below, I expect trouble extracting the value I want.

    Here's what I tried.  I get the error 'There is no row at position such and such'.  I looked at the database itself and indeed there is a row at that position and it contains the data I just updated.

    Let me be frank.  I'm 100% disabled with spastic paraparysis.  I take anti spastic pills, muscle relaxants all day long, and sleeping medicine at night.  I'm not as sharp as I should be.  

    I'd really appreciate the query you're suggesting and code that actually extracts the value I'm looking for.

    dennist

           Dim AT As String = Me.AuthorTypeTextBox.Text
            Dim Active As Boolean = Me.ActiveCheckBox.Checked
            Me.AuthorTypeTableAdapter.InsertQuery("New5", True)
            Me.AuthorTypeTableAdapter.Fill(DsHasbara.AuthorType)
            'Me.AuthorTypeBindingSource.MoveLast()
            Dim Icount As Integer = Me.AuthorTypeBindingSource.Count
            'Dim tbl As DataTable = DsHasbara.AuthorTypeDataTable
            Dim tbl As New dsHasbara.AuthorTypeDataTable
            Dim int1 As Integer
            Dim s As String
            int1 = tbl(Icount - 1).ID
            MsgBox(int1.ToString)




           Dim AT As String = Me.AuthorTypeTextBox.Text
            Dim Active As Boolean = Me.ActiveCheckBox.Checked
            Me.AuthorTypeTableAdapter.InsertQuery("New5", True)
            Me.AuthorTypeTableAdapter.Fill(DsHasbara.AuthorType)
            'Me.AuthorTypeBindingSource.MoveLast()
            Dim Icount As Integer = Me.AuthorTypeBindingSource.Count
            'Dim tbl As DataTable = DsHasbara.AuthorTypeDataTable
            Dim tbl As New dsHasbara.AuthorTypeDataTable
            Dim int1 As Integer
            Dim s As String
            int1 = tbl(Icount - 1).ID
            MsgBox(int1.ToString)

  • Prince_san

    I assumed you had a fully configured data adapter.  The point is to give the new cmd command object the same connection settings as an exiting command that works.  You can replace Me.AuthorTypeTableAdapter.SelectCommand.Connection with the name of your connection object directly, or change SelectCommand to InsertCommand or some other command that is defined on the DataAdapter.
  • Dimitris Sv

    I've decided to do it the ado.net way.  I've dragged and dropped the topic table onto a form, with the bindingNavigator on top.  It may take a day or two to work out the kinks.

    You mentioned in a previous posting Just keep a reference to the DataRow object when it is created.

    But this won't apply when I click the save button on the Navigator. Is there a way I can capture the new value, without clicking movelast and then having a variable capture the value of the text in a label

    Thank you for all the hard work.

    dennist685

  • L M

    Here's the code you've requested:

            'Create a new SQL Command from your existing DataAdapter's connection
            Dim cmd As System.Data.SqlClient.SqlCommand = AuthorTypeTableAdapter.SelectCommand.Connection.CreateCommand
            'Set the type to text
            cmd.CommandType = CommandType.Text

            'This is the SQL statement that will get the highest Index value

            '!!!Be sure to replace "ColumnName" with the name of your index column
            'and TableName with the name of your table!!!
            cmd.CommandText = "SELECT MAX([ColumnName]) AS NewID FROM [TableName]"

            'Open the connection
            cmd.Connection.Open()

            'Execute the query. Scalar works great because we only need one value
            Dim NewIndex As Integer = CType(cmd.ExecuteScalar, Integer)

            'Close the connection
            cmd.Connection.Close()

            MessageBox.Show("The new Index value is: " & NewIndex.ToString, "Record Added", MessageBoxButtons.OK, MessageBoxIcon.Information)


    It looks like you're following the classic ADO model and not really taking advantage of ADO.Net...  The new model would say to fill your DataSet, add a new row to a table in the DataSet, and then use the Update() method of the DataAdapter to get all changes (inserts, updates, deletes) back to the database.  Using this method you could directly read the value of the index field on the new row after adding it to the table.  Just keep a reference to the DataRow object when it is created.

  • PierreT

    It's about 2.30 AM here and I'm a bit bleary eyed, so I apologise if I've overlooked something important in the previous posts that I've only half read, but "SELECT @@IDENTITY" returns the last system generated identity value.  @@IDENTITY is a global variable, so if your database may be being updated from other clients you may want to use SCOPE_IDENT or IDENT_CURRENT instead.  Check the MSDN library for details of each.
  • how can I retrieve a newly added autoincremented key index?