943,926 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3276
  • C# RSS
Apr 23rd, 2008
0

<Help!!> I need to retrieve an autonumber from microsoft access using visual C#

Expand Post »
I am working on my senior project and I am using C#.net and Microsoft Access to create a program that lets the user store information into a database. I have many tables but the primary table that mostly everything connects to is the customers table. Well I know how to insert, update and delete using C#. But I need to know how to retrieve the primary key from the customers table which happens to be an autonumber. I kind of understand that I need to use SCOPE_IDENTITY or @@IDENTITY. I just have no idea how. I have tried a couple ways without any success.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
precado999 is offline Offline
3 posts
since Sep 2007
Apr 24th, 2008
0

Re: <Help!!> I need to retrieve an autonumber from microsoft access using visual C#

Do your Insert Query, the do a SELECT @@IDENTITY Query immediately afterward. It's best to do both in a stored procedure with the Identity as an OUT parameter.

Try this link: http://www.netomatix.com/adonetauto.aspx
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Apr 24th, 2008
0

Re: <Help!!> I need to retrieve an autonumber from microsoft access using visual C#

Ill give it a shot.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
precado999 is offline Offline
3 posts
since Sep 2007
Apr 24th, 2008
0

Re: <Help!!> I need to retrieve an autonumber from microsoft access using visual C#

Let us know. Remember to mark the thread solved if this answered your question.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Apr 25th, 2008
0

Re: <Help!!> I need to retrieve an autonumber from microsoft access using visual C#

I've always run into issues with using the IDENTITY property in Access. If your using an autonumber, you could always just do something like:

SELECT MAX(autonumber_column_name) AS alias FROM table_name

Do that after your insert and it should return the last autonumber generated.
Reputation Points: 23
Solved Threads: 10
Light Poster
bcasp is offline Offline
45 posts
since Apr 2008
Apr 25th, 2008
0

Re: <Help!!> I need to retrieve an autonumber from microsoft access using visual C#

Hi

If your DB going to be affected by several procedures or triggers at the same time into your session, you have to use SCOPE_IDENTITY().

Example:

CREATE PROCEDURE prTable_Insert
@value1 varchar(16)

AS
INSERT INTO Table(field1) VALUES (@value1)
SELECT SCOPE_IDENTITY()
GO

This going to return the last value inserted by this store procedure, but if you use @@IDENTITY this going to return the last ID inserted in your sesion, and this value could be another inserted by another process or trigger out of this procedure executed in your actual session.

Good luck!!!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
imartinez is offline Offline
1 posts
since Apr 2008
Apr 25th, 2008
0

Re: <Help!!> I need to retrieve an autonumber from microsoft access using visual C#

thanks I have figured it out thanks to all the replys.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
precado999 is offline Offline
3 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: how to add database into the while iam deploying.
Next Thread in C# Forum Timeline: Changing from Delphi to C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC