2010-05-20 164 views
0

喜想写的,我需要进行更新,但在主键码我如何实现它 我写了下面的代码:请看看它让我知道在哪里错了违反PRIMARY KEY约束“PK_

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 [email protected],[email protected],[email protected] 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是我的主键

回答

0

那么,什么是你的问题?它不是的代码 - 代码工作(你没有得到语法错误)。 PK违规是数据级别违规。基本上你会尝试输入一个无效的值,double值,无论出现哪个主键无效。

不知道表结构,关系和数据,我们真的不能回答这个问题。

+0

更新心不是hapenning它给我一个错误 有两个表 在我有位置ID,CSV代码和kubelocation 和另一个我已经locationid和位置,有位置id作为我的两个表中的主键 – abhi 2010-05-20 09:31:57

0

UPDATE语句具有在类似下面的子句谓词:

where Locationid not in (select Locationid from MstKMSLocKubeLocMapping) 

而且,我意识到这是一个有点笨拙询问你的设计,而不是仅仅回答这个问题,但你真的不应该有更新主键。

+0

所以如何我做到了吗? – abhi 2010-05-20 09:38:34

+0

并称,谓词到你的where子句应该修复它。我不确定为什么设计需要更新密钥。 – 2010-05-20 10:04:41

相关问题