trying to save data I put in dataset into a linq table RRS feed

  • Question


    Hello everybody,

    What I I'm trying to do is this: I have 3 tables in a local sql compact .sdf file, CodeViolation which is the parent table, CodeViolationImages, and CodeViolationDocuments which are children of CodeViolation. When I did this before I used an identity column this worked perfectly but now that I have an Identity column I just cant get it to save correctly. I have gathered the data using the dataset that was created when I created the sync file. but I can't seem to copy that data from the dataset to the database file without messing up the primary key.

    heres the code I'm using for the save button. Thanks in advance to anyone that can give me a hand with this.



        Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

            Dim PlanEnforce As New Planning_Enforcement(My.Settings.cnPlanning_EnforcementFullPath)
            Dim Imagecounter As Int32 = 0
            Dim DocTable As DataTable = plan.CodeViolationDocuments
            Dim ImageTable As DataTable = plan.CodeViolationImages
            Dim newviolationimages As New CodeViolationImages
            Dim newviolationdocuments As New CodeViolationDocuments


           ' these functions grab the new row(s) that I have added to them 



                'if the new row is saved
                If cancelkey = 1 Then
                    Dim violation As New CodeViolation

                    With violation

                        .FirstName = txtFirstName.Text
                        .LastName = txtLastName.Text
                        .Address1 = txtAddress1.Text
                        .Address2 = txtAddress2.Text
                        .City = txtCity.Text
                        .State = cboState.Text
                        .ZipCode = txtZipCode.Text
                        .PhoneNumber = txtTelephone.Text
                        .Comments = txtComments.Text
                        .ParcelID = txtParcelID.Text
                        .CodeType = cboType.Text
                        .Qty = CInt(numQty.Text)
                        .Description = txtDiscription.Text
                        .District = cboDistrict.Text
                        .AssignedTo = cboAssignedTo.Text
                        .ReportedBy = txtReportedBy.Text
                        .DateReceived = CDate(dtDateReceived.Text)
                        .DateSiteVisit = CDate(dtDateSiteVisit.Text)

                    End With

                    'When It gets to here it gives me the error:
                    'A duplicate value cannot be inserted into a unique index. [ Table name = CodeViolation,Constraint name = PK_CodeViolation ]

                    'Code Violation Images Columns

                    'if theres no rows then skip images
                    If Not ImageTable.Rows.Count = 0 Then

                        'For Each ImageRow In ImageTable.Rows

                        For Each row In ImageTable.Rows

                            newviolationimages.Active = CBool(plan.CodeViolationImages.Rows(0).Item("Active"))
                            newviolationimages.CodeViolationKey = CInt(plan.CodeViolationImages.Rows(0).Item("CodeViolationKey"))
                            newviolationimages.CreateDate = CType(plan.CodeViolationImages.Rows(0).Item("CreateDate"), Date)
                            newviolationimages.CreateUser = plan.CodeViolationImages.Rows(0).Item("CreateUser").ToString
                            newviolationimages.ImagePath = plan.CodeViolationImages.Rows(0).Item("ImagePath").ToString
                            newviolationimages.UpdateDate = Date.Now
                            newviolationimages.UpdateUser = plan.CodeViolationImages.Rows(0).Item("UpdateUser").ToString




                    End If

                    'if there are no rows in docrow then skip documents
                    If Not DocTable.Rows.Count = 0 Then

                        For Each docrow In DocTable.Rows
                            'plan.CodeViolationDocuments. = newviolationdocuments

                            newviolationdocuments.Active = CBool(plan.CodeViolationDocuments.Rows(0).Item("Active"))
                            '  newviolationdocuments.CodeViolationDocumentsKey = CInt(plan.CodeViolationDocuments.Rows(0).Item("CodeViolationDocumentsKey"))
                            newviolationdocuments.CodeViolationKey = CInt(plan.CodeViolationDocuments.Rows(0).Item("CodeViolationKey"))
                            newviolationdocuments.CreateDate = CType(Date.FromOADate(1 / 12 / 1111), DateTime) 'CType(plan.CodeViolationDocuments.Rows(0).Item("CreateDate"), Date)
                            newviolationdocuments.CreateUser = plan.CodeViolationDocuments.Rows(0).Item("CreateUser").ToString
                            'newviolationdocuments.CreationDate = CDate(newviolation.CreationDate) 'CType(plan.CodeViolationDocuments.Rows(0).Item("CreationDate"), Date)
                            newviolationdocuments.DocDescription = plan.CodeViolationDocuments.Rows(0).Item("DocDescription").ToString
                            newviolationdocuments.DocPath = plan.CodeViolationDocuments.Rows(0).Item("DocPath").ToString

                            newviolationdocuments.UpdateDate = CType(Date.FromOADate(1 / 12 / 1111), DateTime)

                            newviolationdocuments.UpdateUser = plan.CodeViolation.Rows(0).Item("UpdateUser").ToString
                            ' newviolationdocuments = CType(plan.Tables("CodeViolationDocument"), WpfCodeViolation.CodeViolationDocuments)



    end sub


    Tuesday, July 29, 2008 12:22 PM