Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Insert Records create Duplicate Records

Posted on 2010-09-10
3
Medium Priority
?
270 Views
Last Modified: 2012-05-10
The code below is inserting additional data in another personal user information table.  however it keeps creating more than one record upon insert.  Why? and How do I fix it?
Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs)
        'Insert Data into table and Add other pertinent data
        Dim UserNameTextBox As TextBox = CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName")
        Dim createDate As Date = DateTime.Now.ToString("MM/dd/yy")

        Dim rowsAffected As Integer
        Dim rIdentity As Integer = -1
        Try
            Dim myConnectionString As String
            Dim myQuery As String
            Dim myConnection As SqlConnection
            Dim cmd As SqlCommand


            myConnectionString = "Data Source=ServerIP;Initial Catalog=dbname; User Id=username; Password=password;"
            myQuery = "INSERT INTO aspnet_UserName (UserName, Firstname, Lastname, Prefixname, Suffixname, Nickname, " _
                & "CreateDate, AccountStatus, OrgNameID) VALUES ('" _
                & UserNameTextBox.Text & "', '" & Firstname.Text & "', '" & Lastname.Text & "', '" & PrefixnameDDL.Text & "', '" _
                & Suffix.Text & "', '" & Nickname.Text & "', '" & createDate & "', 'Enabled', '" _
                & getOrgNameID(OrganizationDDL.Text) & "') SELECT SCOPE_IDENTITY();"
            myConnection = New SqlConnection(myConnectionString)
            myConnection.Open()
            cmd = New SqlCommand(myQuery, myConnection)
            rowsAffected = cmd.ExecuteNonQuery()
            rIdentity = Integer.Parse(cmd.ExecuteScalar().ToString()) 'Get the @Identity Column

            myConnection.Close()
        Catch ex As Exception
            ClientScript.RegisterStartupScript(Me.GetType(), "alert", "<script language='javascript'>alert('CreateUserWizard1_CreatedUser produce the following error: " + ex.Message + "');</script>")
            Response.Write(ex.Message)
        End Try
    End Sub

Open in new window

0
Comment
Question by:Robert Treadwell
[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
3 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33651225
one of two ways perhaps

create a unique index so that the operation fails

or

read the table, check the record exists or not
if it exists then update else insert
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 33651245
1. You are using the CreateUserWizard.
2. When you use this wizard, the CreateUserWizard1_CreatedUser event only fires AFTER the user has already been created.
3. You are doing an insert there... which is creating the duplicate user.

With the CreateUserWizard, the user is automatically created for you, you dont need to execute an INSERT.

Comment out all the code in this method, compile and attempt to create a user. You will see that the duplicate users are gone.
0
 
LVL 5

Accepted Solution

by:
David Christal CISSP earned 2000 total points
ID: 33651712
If MlandaT's suggestion doesn't work out try it with line 24 commented out.
Looks like you're executing the insert on both 24 & 25
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 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