Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good day,

I am very new to development, so would appreciate any help.

I am curently trying to work out how to retrieve an ID from My database after I have added a record using a webform compiled of c# in ASP.net.

scenario : Create an order, and upon creation the page will post back the Order Number.

Kind regards,

JD
Posted

Yes possible, using SCOPE_IDENTITY()
In ADO.NET, one can send multiple queries in one go or call a stored procedure.

For quick start try:
C#
myCommandForData.CommandText = "INSERT INTO [Country] (CName) VALUES (@CNameValue); SELECT SCOPE_IDENTITY()";
int newCountryID = (int)myCommandForData.ExecuteScalar();


Handling it in an SP and calling an SP would be a better way to use it.

I would suggest you to read on ADO.NET here:
Look at these:
MSDN: ADO.NET[^]
MSDN: Accessing Data with ADO.NET[^]

SP specific, go here:
MSDN: HOW TO: Call SQL Server Stored Procedures in ASP.NET by Using Visual C# .NET[^]
MSDN: HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET[^]
ADO.NET : Using Stored Procedures[^]
 
Share this answer
 
Hi,

This depends upon how you access the datalayer / your database:




I hope this gives you some starting points to solve your problem.


Best regards and happy coding,
Stops
 
Share this answer
 
v2
Comments
Member 7949758 24-May-11 7:04am    
Thanks for the quick replies.

All solutions are of use but im not really sure what is the best choice.

I use OpenAccess to acces my data layer, And use C# to write to database exc.

I have tried going down the route of creating a stored procedure. And then using Scope_Identity() to call it.

Any advise or example code you could Suggest ?

I would try to better explain myself but i come from a background in Database design not implementation...

I have just finsished University in a degree which was not related to Programming so I am a complete Newbie to it.

And im trying my best to teach myself using c#
Christoph Keller 24-May-11 8:29am    
Hi,

Please look at the links in the Solution 1 from S Mevara. There you will find the stored procedure (SP) related links. In your case, I would prefer a SP which have one output declared parameter (see Using a Stored Procedure with Output Parameters). And inside the SP you write something like: SET @outputParameterName = SCOPE_IDENTITY()

Hope this helps.

Best regards and good luck,
Stops
This[^] will get you started.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900