Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

839 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