|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Please could some1 help.
I have a OleDbConnection, to an access database (jet4) OleDbCommand then than Inserts a record - which works. For each record a Autonumber primary key is created. Now what i want is for the newly created id to get brought back and showed on the form.? Heres my code to insert. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '----------------------------------------------- OleDbCommand1.Parameters("Name").Value = TextBox.Text '----------------------------------------------- Dim SQLreturn As Integer 'OPEN THE CONNECTION AND EXECUTE THE COMMAND Try OleDbConnection1.Open() SQLreturn = OleDbCommand1.ExecuteNonQuery() Catch ex As Exception MessageBox.Show(ex.Message) End Try 'END - SHOW EX MESSAGE IF NEED BE '----------------------------------------------- '----------------------------------------------- 'TEST IF STUFF WENT IN If SQLreturn <> 1 Then MessageBox.Show("Could not execute the INSERT query", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Else MessageBox.Show("Database Update Sucsesfull", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Information) End If OleDbConnection1.Close() 'END ME TEST - CLOSE THE CONNECTION '----------------------------------------------- End Sub |
|
#2
|
|||
|
|||
|
One solution is to use stored procedures and have an output parameter in wich you select the latest [indexfield] in the table.
... @newID INT out INSERT INTO Whatever(stuff1,stuff2) VALUES('one','two') SELECT @newID = MAX([indexfield]) FROM Whatever ... Then you can pick this up in the code. Hope this helps. /Majk |
![]() |
| Viewing: Dev Articles Community Forums > Programming > .NET Development > help! - Getting Primary Key |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|