.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 23rd, 2004, 07:33 AM
gaaz gaaz is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 gaaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy help! - Getting Primary Key

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


Reply With Quote
  #2  
Old May 3rd, 2004, 07:42 AM
majk majk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 3 majk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgramming.NET Development > help! - Getting Primary Key


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT