【问题标题】:Handle Listview insert duplicate sql error ASP.NET处理 Listview 插入重复的 sql 错误 ASP.NET
【发布时间】:2014-11-11 14:19:16
【问题描述】:

这是我第一次提出问题,我一直在寻找 2 天来找到答案。希望各位高手能提供帮助。

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

下面是我最希望的……但没有。

仍然收到“违反主键约束“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=server\instance; 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

任何建议将不胜感激!谢谢。

【问题讨论】:

    标签: asp.net vb.net listview sql-server-2012


    【解决方案1】:

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

    Catch ex As Exception 
    

    【讨论】:

    • 谢谢,但还是没有运气。仍然遇到丑陋的错误。
    【解决方案2】:

    我是正式的白痴。

    如果我记得这样写这行(即添加 Handles blah blah),我就不会遇到问题了。

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

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 2010-10-17
      • 1970-01-01
      • 2018-09-19
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      相关资源
      最近更新 更多