tags:

views:

184

answers:

2

hi am trying to write a code in which i need to perform a update but on primary keys how do i achieve it i have written the following code: kindly look at it let me know where m wrong

 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

this is my designer page'

<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 is my primary key

A: 

So what is your problem? The CODE it is not - the CODE works (you do not get a syntax error). PK violations are data level violations. Basically you try to put in an invalid value, double value, whatever comes up that is invalid for a primary key.

Without knowing table structures, relations AND THE DATA we can not really answer that.

TomTom
update isnt hapenning its giving me an error there are two tablesin one i have location id,csv code and kubelocationand another i have locationid and location and have location id as my primary key in both tables
abhi
A: 

In the update statement have a predicate in where clause like the following:

where Locationid not in (select Locationid from MstKMSLocKubeLocMapping)

Also, I realize it's a bit gauche to ask about your design instead of just answering the question but you really should not have to update the primary key.

Rodrick Chapman
so how do i do it?
abhi
Adding that predicate to your Where clause should fix it. I'm not sure why the design requires updating the key.
Rodrick Chapman