Tuesday, February 21, 2012

SCOPE_IDENTITY() Help

I was wondering if its possible to use this function if your using aSqlDataReader where I just run a stored procedure that just inserts arow?

Better might be a return value that you get at via a Return Code parameter using ExecuteNonQuery. No reason you could not do a SELECT SCOPE_IDENTITY() at the end of an INSERT SP and then get at it using ExecuteScalar() if you wish.

CREATE PROCEDURE spfoo
@.fooID int
AS
INSERT INTO fooTable(fooID) VALUES(@.fooID)
RETURN SCOPE_IDENTITY()

|||
Hi
Using SQLDataSource and SQL 2005 Stored Procedure I've been successful in creating multiple additional CreateUser fields saving them to "tbl_UserDetails" table.
I pass the UserId to the tbl_UserDetails as well. What I'm struggling with is retreiving the new tbl_UserDetails "Details_Id" - t-sql RETURN SCOPE_IDENTITY()).
I want to integrate it into the Profile data (see Profile creation below in CompleteButton_Click (71)).
I'm not sure if the problem retreiving the Details_Id is in the SQLDSInsert_Inserted (133) code used or if I'm not addressing the retreival in the "correct" stage of "the process". See below.
Thanks
1Imports Telerik.WebControls2Imports SITE.DAL.DBAccess3Imports System.Data4Imports System.Data.SqlClient56PartialClass Site_Pages_Registration7Inherits System.Web.UI.Page8Protected WithEvents Update_DateAs WebControls.HiddenField910Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Load11If Session("Record") IsNotNothing Then12 CreateUserWizard1.MoveTo(CompleteWizardStep1)13End If1415 End Sub1617 Protected Sub CreateUserWizard1_CreatedUser(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles CreateUserWizard1.CreatedUser18Dim userAs MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)1920If userIs Nothing Then21 Throw New ApplicationException("Can't find the user.")22 End If2324 Dim DetailsInsert As SqlDataSource = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("SQLDSInsert"), SqlDataSource)25 Dim UserId As Guid = DirectCast(user.ProviderUserKey, Guid)26 Session("NewUserId") = UserId27 Dim Details_FirstName As TextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_FirstName"), TextBox)28 Dim Details_LastName As TextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_LastName"), TextBox)29 Dim Details_MI As TextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_MI"), TextBox)30 Dim Details_Title As TextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Title"), TextBox)31 Dim Details_Company As TextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Company"), TextBox)32 Dim Details_1Comm As Telerik.WebControls.RadMaskedTextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_1Comm"), Telerik.WebControls.RadMaskedTextBox)33 Dim Details_1CommType As Telerik.WebControls.RadComboBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_1CommType"), Telerik.WebControls.RadComboBox)34 Dim Details_2Comm As Telerik.WebControls.RadMaskedTextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_2Comm"), Telerik.WebControls.RadMaskedTextBox)35 Dim Details_2CommType As Telerik.WebControls.RadComboBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_2CommType"), Telerik.WebControls.RadComboBox)36 Dim Details_Address As TextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Address"), TextBox)37 Dim Details_City As TextBox = CType(CreateUserWizardStep0.ContentTemplateContainer.FindControl("Details_City"), TextBox)38 Dim Details_State As Telerik.WebControls.RadComboBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_State"), Telerik.WebControls.RadComboBox)39 Dim Details_Zip As Telerik.WebControls.RadMaskedTextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Zip"), Telerik.WebControls.RadMaskedTextBox)40 Dim Details_UserTypeId As Telerik.WebControls.RadComboBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_UserTypeId"), Telerik.WebControls.RadComboBox)41 Dim Details_ReceiveEmail As CheckBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_ReceiveEmail"), CheckBox)4243 DetailsInsert.Insert()4445 Dim UserType As Telerik.WebControls.RadComboBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_UserTypeId"), Telerik.WebControls.RadComboBox)46 Session("UserType") = UserType.SelectedValue.ToString47 End Sub4849 Protected Sub SQLDSInsert_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)50 Dim user As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)5152 e.Command.Parameters("@.UserId").Value = user.ProviderUserKey53 e.Command.Parameters("@.Details_FirstName").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_FirstName"), TextBox).Text54 e.Command.Parameters("@.Details_LastName").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_LastName"), TextBox).Text55 e.Command.Parameters("@.Details_MI").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_MI"), TextBox).Text56 e.Command.Parameters("@.Details_Company").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Company"), TextBox).Text57 e.Command.Parameters("@.Details_Title").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Title"), TextBox).Text58 e.Command.Parameters("@.Details_1Comm").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_1Comm"), Telerik.WebControls.RadMaskedTextBox).Text59 e.Command.Parameters("@.Details_1CommType").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_1CommType"), Telerik.WebControls.RadComboBox).SelectedValue60 e.Command.Parameters("@.Details_2Comm").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_2Comm"), Telerik.WebControls.RadMaskedTextBox).Text61 e.Command.Parameters("@.Details_2CommType").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_2CommType"), Telerik.WebControls.RadComboBox).SelectedValue62 e.Command.Parameters("@.Details_Address").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Address"), TextBox).Text63 e.Command.Parameters("@.Details_City").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_City"), TextBox).Text64 e.Command.Parameters("@.Details_State").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_State"), Telerik.WebControls.RadComboBox).SelectedValue65 e.Command.Parameters("@.Details_Zip").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Zip"), Telerik.WebControls.RadMaskedTextBox).Text66 e.Command.Parameters("@.Details_UserTypeId").Value = Int32.Parse(CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_UserTypeId"), Telerik.WebControls.RadComboBox).SelectedValue)67 e.Command.Parameters("@.Details_ReceiveEmail").Value = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_ReceiveEmail"), CheckBox).Checked68 End Sub697071 Protected Sub CompleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)72 Dim user As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)7374 Dim pb As ProfileBase = ProfileBase.Create(user.UserName)75 pb.SetPropertyValue("FirstName", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_FirstName"), TextBox).Text)76 pb.SetPropertyValue("LastName", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_LastName"), TextBox).Text)77 pb.SetPropertyValue("UserName", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName"), TextBox).Text)78 pb.SetPropertyValue("Record", Session("Record"))79 pb.SetPropertyValue("Company", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Company"), TextBox).Text)80 pb.SetPropertyValue("Title", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Title"), TextBox).Text)81 pb.SetPropertyValue("Comm1", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_1Comm"), Telerik.WebControls.RadMaskedTextBox).Text)82 pb.SetPropertyValue("Comm1Type", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_1CommType"), Telerik.WebControls.RadComboBox).SelectedValue)83 pb.SetPropertyValue("Comm2", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_2Comm"), Telerik.WebControls.RadMaskedTextBox).Text)84 pb.SetPropertyValue("Comm2Type", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_2CommType"), Telerik.WebControls.RadComboBox).SelectedValue)85 pb.SetPropertyValue("Address", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Address"), TextBox).Text)86 pb.SetPropertyValue("City", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_City"), TextBox).Text)87 pb.SetPropertyValue("State", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_State"), Telerik.WebControls.RadComboBox).SelectedValue)88 pb.SetPropertyValue("Zip", CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Zip"), Telerik.WebControls.RadMaskedTextBox).Text)8990 pb.Save()9192 Dim lblRegisterCompleteName As Label = CreateUserWizard1.CompleteStep.FindControl("lblRegisterCompleteName"), Label93 lblRegisterCompleteName.Text = Profile.FirstName & " " & Profile.LastName94 Dim Muser As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)95 If Muser Is Nothing Then96 Throw New ApplicationException("Can't find the user.")97End If98 Muser.IsApproved =False99 End Sub100101 Protected Sub btnTeamProfile_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)102Dim userAs MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)103104Dim pbAs ProfileBase = ProfileBase.Create(user.UserName)105 pb.SetPropertyValue("FirstName",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_FirstName"), TextBox).Text)106 pb.SetPropertyValue("LastName",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_LastName"), TextBox).Text)107 pb.SetPropertyValue("UserName",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName"), TextBox).Text)108 pb.SetPropertyValue("Record", Session("Record"))109 pb.SetPropertyValue("Company",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Company"), TextBox).Text)110 pb.SetPropertyValue("Title",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Title"), TextBox).Text)111 pb.SetPropertyValue("Comm1",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_1Comm"), Telerik.WebControls.RadMaskedTextBox).Text)112 pb.SetPropertyValue("Comm1Type",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_1CommType"), Telerik.WebControls.RadComboBox).SelectedValue)113 pb.SetPropertyValue("Comm2",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_2Comm"), Telerik.WebControls.RadMaskedTextBox).Text)114 pb.SetPropertyValue("Comm2Type",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_2CommType"), Telerik.WebControls.RadComboBox).SelectedValue)115 pb.SetPropertyValue("Address",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Address"), TextBox).Text)116 pb.SetPropertyValue("City",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_City"), TextBox).Text)117 pb.SetPropertyValue("State",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_State"), Telerik.WebControls.RadComboBox).SelectedValue)118 pb.SetPropertyValue("Zip",CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Details_Zip"), Telerik.WebControls.RadMaskedTextBox).Text)119120 pb.Save()121 Response.Redirect("Profile.aspx")122End Sub123124 Protected Sub CompleteWizardStep1_PreRender(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles CompleteWizardStep1.PreRender125Dim btnTeamProfileAs Button =CType(CompleteWizardStep1.ContentTemplateContainer.FindControl("btnTeamProfile"), Button)126If Session("UserType") ="2"Then127 btnTeamProfile.Visible =True128 Else129 btnTeamProfile.Visible =False130 End If131 End Sub132133 Protected Sub SQLDSInsert_Inserted(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)134Dim DetailsInsertAs SqlDataSource =CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("SQLDSInsert"), SqlDataSource)135Dim RecordAs Integer = DetailsInsert.InsertParameters.Add("@.Details_Id", System.Data.ParameterDirection.ReturnValue)136'Dim Record As Integer = e.Command.ExecuteScalar137 Session("Record") = Record.ToString138139 Response.Redirect("Registration.aspx")140End Sub141142143End Class144

No comments:

Post a Comment