none
How can I make insert by LINQ to SQL? RRS feed

  • Question

  • I have little problem, cause when I try to insert by LINQ:

            Dim db As New DataBaseDataContext(sqlConnection)

            Dim nw As New wydarzenia

       

            nw.wydarzenie_kat_id = ddl1.SelectedValue
            nw.wydarzenie_tytul = tytul.Text
            nw.miasto_id = 288
            nw.wydarzenie_miejsce = miejsce.Text
            nw.wydarzenie_ulica = ulica.Text
            nw.wydarzenie_oplaty = oplaty.SelectedValue
            nw.wydarzenie_miniatura = "asd"
            nw.wydarzenie_aktywne = 0

            Try

                db.wydarzenias.InsertOnSubmit(nw)
                db.SubmitChanges()

            Catch ex As Exception
                Response.Write(ex.ToString)
            End Try


    But Im getting error

    System.InvalidCastException: Określone rzutowanie jest nieprawidłowe. w System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager`2.TryCreateKeyFromValues(Object[] values, V& v) w System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[] keyValues) w System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues) w System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues) w System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance) w System.Data.Linq.ChangeProcessor.BuildEdgeMaps() w System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) w System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) w System.Data.Linq.DataContext.SubmitChanges() ....

    Can anyone explain me how to insert data?
    Sunday, April 6, 2008 1:44 PM

Answers

  • Could you please post the DBML or code generated for the wydarzenia class.  It does sound like a foreign key or other SQL constraint is not being met.

     

    [)amien

     

     

    Wednesday, May 28, 2008 9:00 PM
    Moderator

All replies

  • Can you please translate the exception text into English?

     

    Thanks,

     

    --Samir

     

    Tuesday, April 8, 2008 9:19 PM
  • O realy don't understand what exception says, but I see there a class IdentityManager... are you afecting values to a identity column? are one of your column relation to another tables, if so check if value exists.

    That is the right way to insert.
    Friday, April 11, 2008 7:26 PM
  • Could you please post the DBML or code generated for the wydarzenia class.  It does sound like a foreign key or other SQL constraint is not being met.

     

    [)amien

     

     

    Wednesday, May 28, 2008 9:00 PM
    Moderator