Solved

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

Posted on 2013-11-12
4
425 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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

15 Experts available now in Live!

Get 1:1 Help Now