Solved

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

Posted on 2013-11-12
4
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to handle time out inside the stored procedure 10 28
sql server cross db update 2 23
SQL Syntax 6 33
Importing cr/lf as text in sql server 7 18
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

730 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