我正在尝试向我的用户登录添加额外数据。我已经按照教程,但一直收到主键错误

时间:2013-12-01 22:21:36

标签: asp.net sql vb.net vba login

我正在违反PRIMARY KEY约束'PK_CLIENT'。无法在对象'dbo.CLIENT'中插入重复键。每次我尝试创建一个新用户。我目前的代码,据我所知,模仿教程Here。我的代码: `

Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles CreateUserWizard1.CreatedUser
    ' Get the UserId of the just-added user
    Dim newUser As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)
    Dim newUserId As Guid = DirectCast(newUser.ProviderUserKey, Guid)

    'Get Profile Data Entered by user in CUW control

    Dim FirstName As String = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FirstName"), TextBox).Text
    Dim LastName As String = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("LastName"), TextBox).Text
    Dim Phone As String = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Phone"), TextBox).Text
    Dim Email As String = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Email"), TextBox).Text

    ' Insert a new record into User_Profile

    ' Get your Connection String from the web.config. MembershipConnectionString is the name I have in my web.config
    Dim connectionString As String = ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString").ConnectionString

    Dim insertSql As String = "INSERT INTO CLIENT(UserId, FirstName, LastName, Phone, Email) VALUES(@UserId, @FirstName, @LastName, @Phone, @Email)"
    Using myConnection As New SqlConnection(connectionString)
        myConnection.Open()
        Dim myCommand As New SqlCommand(insertSql, myConnection)
        myCommand.Parameters.AddWithValue("@UserId", newUserId)
        myCommand.Parameters.AddWithValue("@FirstName", FirstName)
        myCommand.Parameters.AddWithValue("@LastName", LastName)
        myCommand.Parameters.AddWithValue("@Phone", Phone)
        myCommand.Parameters.AddWithValue("@Email", Email)

        myCommand.ExecuteNonQuery()
        myConnection.Close()
    End Using
End Sub

`

任何帮助解决此错误都将不胜感激。

1 个答案:

答案 0 :(得分:0)

每个新客户端都需要一个新的Guid。您的方法为您提供了相同的Guid