违反PRIMARY KEY约束'PK_

时间:2010-05-20 09:08:41

标签: asp.net sql vb.net

您正在尝试编写一个代码,我需要在其中执行更新但在主键上如何实现它 我写了下面的代码:请看看它让我知道哪里错了

 Protected Sub rgKMSLoc_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgKMSLoc.UpdateCommand
        Try
            KAYAReqConn.Open()


            If TypeOf e.Item Is GridEditableItem Then

                Dim strItemID As String = CType(e.Item.FindControl("hdnID"), HiddenField).Value
                Dim strrcmbLocation As String = CType(e.Item.FindControl("rcmbLocation"), RadComboBox).SelectedValue
                Dim strKubeLocation As String = CType(e.Item.FindControl("txtKubeLocation"), TextBox).Text
                Dim strCSVCode As String = CType(e.Item.FindControl("txtCSVCode"), TextBox).Text

                SQLCmd = New SqlCommand("SELECT * FROM MstKMSLocKubeLocMapping WHERE LocationID= '" & rcmbLocation.SelectedValue & "'", KAYAReqConn)

                Dim dr As SqlDataReader
                dr = SQLCmd.ExecuteReader
                If dr.HasRows Then
                    lblMsgWarning.Text = "<font color=red>""User ID Already Exists"

                    Exit Sub
                End If
                dr.Close()


                SQLCmd = New SqlCommand("UPDATE MstKMSLocKubeLocMapping SET LocationID=@Location,KubeLocation=@KubeLocation,CSVCode=@CSVCode WHERE LocationID = '" & strItemID & "'", KAYAReqConn)
                SQLCmd.Parameters.AddWithValue("@Location", Replace(strrcmbLocation, "'", "''"))
                SQLCmd.Parameters.AddWithValue("@KubeLocation", Replace(strKubeLocation, "'", "''"))
                SQLCmd.Parameters.AddWithValue("@CSVCode", Replace(strCSVCode, "'", "''"))
                SQLCmd.Parameters.AddWithValue("@Status", "A")
                SQLCmd.ExecuteNonQuery()
                lblMessageUpdate.Text = "<font color=blue>""Record Updated SuccessFully"
                SQLCmd.Dispose()
                rgKMSLoc.Rebind()

            End If
        Catch ex As Exception
            Response.Write(ex.ToString)
        Finally
            KAYAReqConn.Close()

        End Try
    End Sub

这是我的设计师页面'

<EditFormSettings EditFormType="Template">
                    <FormTemplate>
                        <table border="0" cellpadding="3" cellspacing="0" align="center">
                            <tr class="tableRow">
                                <td class="tableCell">
                                    Location:
                                </td>
                                <td class="tableCell">
                                    <asp:HiddenField ID="hdnID" runat="server" Value='<%# bind("LocationID") %>' />
                                    <telerik:RadComboBox ID="rcmbLocation" runat="server" Text='<%# bind("LocationID") %>'
                                        DataSourceID="dsrcmbLocation" DataTextField="Location" DataValueField="LocationID"
                                        Height="150px">
                                    </telerik:RadComboBox>
                                    <asp:RequiredFieldValidator ID="rfvDesc" ControlToValidate="rcmbLocation" ErrorMessage="Please Select the Clinic"
                                        runat="server" ValidationGroup="Update" Text="*"></asp:RequiredFieldValidator>
                                </td>
                            </tr>
                               <tr class="tableRow">
                                <td class="tableCell">
                                    Kube Location:</td>
                                <td class="tableCell">
                                    <asp:TextBox ID="txtKubeLocation" runat="server" Text='<%# bind("KubeLocation") %>' Class="forTextBox"
                                        MaxLength="4" onkeypress="return filterInput(2,event);">
                                    </asp:TextBox>
                                    <asp:RequiredFieldValidator ID="rfvKubeLoc" ControlToValidate="txtKubeLocation" ErrorMessage="Please Enter KubeLocation"
                                        runat="server" ValidationGroup="Update" Text="*"></asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr class="tableRow">
                                <td class="tableCell">
                                    CSV Code:</td>
                                <td class="tableCell">
                                    <asp:TextBox ID="txtCSVCode" runat="server" Text='<%# bind("CSVCode") %>' Class="forTextBox"
                                        MaxLength="4" onkeypress="return filterInput(2,event);">
                                    </asp:TextBox>
                                    <asp:RequiredFieldValidator ID="rfvCSVCode" ControlToValidate="txtCSVCode" ErrorMessage="Please Enter the CSV Code"
                                        runat="server" ValidationGroup="Update" Text="*"></asp:RequiredFieldValidator>
                                </td>
                            </tr>

                            <tr class="tableRow">
                                <td colspan="2" align="center" class="tableCell">
                                    <asp:ImageButton ID="btnUpdate" runat="server" CommandName="Update" CausesValidation="true"
                                        ValidationGroup="Update" ImageUrl="~/Images/update.gif"></asp:ImageButton>
                                    <asp:ImageButton ID="btnCancel" runat="server" CausesValidation="false" CommandName="Cancel"
                                        ImageUrl="~/Images/No.gif"></asp:ImageButton>
                                </td>
                            </tr>
                        </table>
                    </FormTemplate>
                </EditFormSettings>

Locationid是我的主键

2 个答案:

答案 0 :(得分:0)

那你的问题是什么?它不是CODE - CODE有效(你没有得到语法错误)。 PK违规是数据级别违规。基本上你试图输入一个无效的值,双值,无论出现什么对主键无效。

在不知道表格结构,关系和数据的情况下,我们无法真正回答这个问题。

答案 1 :(得分:0)

在update语句中,where子句中有一个谓词,如下所示:

where Locationid not in (select Locationid from MstKMSLocKubeLocMapping)

另外,我意识到询问你的设计而不仅仅是回答这个问题有点夸张,但你真的不应该更新主键。

相关问题