Solved

What is the ASP.NET method that retrieves the lastly inserted primary key?

Posted on 2013-11-12
4
427 Views
Last Modified: 2013-11-13
Hi experts!

I want to use this function:
WebSecurity.CreateUserAndAccount(User_Profile,username,null,false);

It creates a record in the User_Profile table and in the webpages_Membership.
But I also need to create a record in another table using the id recently inserted.

How do I do this?

stackoverflow.com/questions/9477502/get-the-last-inserted-row-id-with-sql-statement

But it doesn’t apply here because I don’t have access to the procedure of WebSecurity.CreateUserAndAccount()

I remember seeing a page of ASP.NET methods on the ASP.NET website's learning section, in which one method allows me to retrieve the last inserted ID, but I can’t find it.
Anyone know what method it is that does this? Or an alternative way?

Thanks!
0
Comment
Question by:nvs_victor
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 39643932
The CreateUserAndAccount() method doesn't return the ID, but there is a second method that will return the ID for a given username:
int userID = WebSecurity.GetUserId(username)

Open in new window

0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 250 total points
ID: 39644385
carl_tawn's answer is correct in terms of the method to call.

But reviewing, think it will fail because your username in WebSecurity.CreateUserAndAccount(User_Profile,username,null,false); is really the password parameter methinks...

So, just to clarify some possible confusion, that other reference (in the question header) talks about SQL commands like scope_identity() but are you really dealing with SQL procedures or functions, or tables.

You are using websecurity methods : http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity_methods(v=vs.111).aspx

If you are talking of SQL functions, but using websecurity mthods then maybe you need a different approach. It depends on what you are really doing. Of course, being a DB behind it all (well, not sure that is exclusively true) then you do have some SQL strategies as well...

From that msdn link above, check out the syntax for WebSecurity.CreateUserAndAccount()and you will see that your params (User_Profile,username,null,false) do not match the username, password, property and confirmation params per the manual.

You might want to familiarise your self with the websecurity class : http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity(v=vs.111).aspx

It does talk a bit about some interactions and you might want to read the last couple of paragraphs before the example.

So, if you are planning to use the user ID for "another table" (assuming a DB) then maybe your requirement is a little different and you might want to post another question to help clarify some of your plans and the approach, and the websecurity methods.

Sorry about any confusion, but it does seem to be needing clarification based on the terms you have used, the link, some param mismatch and the desire to use the ID for other uses.
0
 

Author Comment

by:nvs_victor
ID: 39644985
Hi all,

first, thank you all for helping!

@carl_tawn,
Thank you! I think this would be a great solution to my problem. The userName is unique, so it'll work well.

@mark_wills,
Thank you for taking the time to help! I apologize because I mixed up two different method parameters into one by mistake when posting on EE (though I have it correct in my code).

The correct parameters for the method is indeed this:
WebSecurity.CreateUserAndAccount(userName,password,null,false);

"If you are talking of SQL functions, but using websecurity mthods then maybe you need a different approach. It depends on what you are really doing. Of course, being a DB behind it all (well, not sure that is exclusively true) then you do have some SQL strategies as well..."
Yes, based on carl_tawn's approach, I can do it either way, both an inline SQL select statement or an ASP.NET method that carl_tawn pointed out, since the userName is unique.

Also thank you for so much for your very polite response, despite my mistake! :)

Closing the question, thanks!
0
 

Author Closing Comment

by:nvs_victor
ID: 39644994
I hope the points awarded make sense. Thanks a lot!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now