句柄Listview插入重复的sql错误ASP.NET



这是我第一次问问题,我已经搜索了两天才找到答案。希望你们中的一位大师能够提供帮助。

我有一个从sql表填充的4列列表视图。该表不允许重复。我只想能够捕捉到这个错误,并向用户提供一条友好的消息,告诉用户该项目已经在列表中了。我发现了多个不起作用的解决方案(可能是我的错误)。

下面是我最希望的。。。但什么都没有。

仍在获取"违反PRIMARY KEY约束'PK_tbl.associateNames'。无法在对象'dbo.tbl.associateNames'中插入重复键"。声明已终止"

   Protected Sub listviewInserting_itemInserted(ByVal sender As Object, ByVal e As ListViewInsertEventArgs)

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Try
        con.ConnectionString = "Data Source=serverinstance; Initial Catalog=sears;User Id=johnnyFive;Password=alive;"
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "INSERT INTO [tbl.associateNames] ([orderIssuer], [associateName], [department]) VALUES (@orderIssuer, @associateName, @department)"
        cmd.ExecuteNonQuery()
    Catch ex As SqlException
        MsgBox("Duplicate Entry found", MsgBoxStyle.Information, "Dups")
        e.Cancel = True
    End Try
End Sub

如有任何建议,我们将不胜感激!谢谢

应该可以。也许是错误的执行类型?如果你把捕获量扩大到会发生什么

Catch ex As Exception 

我是个十足的白痴。

如果我记得这样写这句话(即添加手柄等等),我就不会有问题了。

Protected Sub listviewInserting_itemInserted(ByVal sender As Object, e As ListViewInsertEventArgs) Handles ListView1.ItemInserting
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand

最新更新