how i validate a new line?

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
Marcus
Posts: 7
Joined: Fri 20 Mar 2009 08:42

how i validate a new line?

Post by Marcus » Mon 18 May 2009 20:02

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

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Tue 19 May 2009 07:57

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()

Post Reply