Page 1 of 1

how i validate a new line?

Posted: Mon 18 May 2009 20:02
by Marcus
Hi,

how can i validate a new line?

my problem:
When i add a new line (for example a orderline) then i will check the productnumber. Is the Productnumber wrong then i will remove this line before i do an update or insert on the database.

I tried this but it doesn't worked.
When i call db.Orderlines.DeleteOnSubmit(_CurrentOrderline)
then i became this message:

"SaveRecord: Cannot remove an entity that has not been attached."

Code: Select all

Private Sub iniOrderline()

            Dim Orderlines = From l In db.Orderline _
                             Select l

            myBindingSource.DataSource = Orderlines

            AddHandler myBindingSource.ListChanged, AddressOf BindingSource_ListChanged

        End Sub

        Private Sub BindingSource_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs)

            If e.ListChangedType = System.ComponentModel.ListChangedType.ItemChanged AndAlso _
               e.PropertyDescriptor.Name = "ProductNumber" Then

                _CurrentOrderline = TryCast(Me.myBindingSource.List(e.NewIndex), Orderline)

                If ProductExists(_CurrentOrderline.ProductNumber) Then

                    db.Orderlines.InsertOnSubmit(_CurrentOrderline)
                    db.SubmitChanges()

                Else

                    '// product not exists
                    db.Orderlines.DeleteOnSubmit(_CurrentOrderline) '<- SaveRecord: Cannot remove an entity that has not been attached.
                    db.SubmitChanges()

                End If

            End If

        End Sub
ciao
Marcus

Posted: Tue 19 May 2009 07:57
by AndreyR
In fact, you are trying to delete an item from the context, but you have not attached this item to the context yet.
You can simply not insert the Orderline having incorrect parameters, so just remove the following part of the code.

Code: Select all

Else 

                    '// product not exists 
                    db.Orderlines.DeleteOnSubmit(_CurrentOrderline) '<- SaveRecord: Cannot remove an entity that has not been attached. 
                    db.SubmitChanges()