[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-11-12
4
Medium Priority
?
461 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 1000 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 1000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

649 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