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

how can I retrieve a newly added autoincremented key index?
ChrisDurbin
rwindsley
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
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
Dimitris Sv
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
'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