none
Saving changes in WPF DataGrid back to my database using LINQ to SQL RRS feed

  • Question

  • I’m using Microsoft’s Windows 7, VS 2010 Express, Windows Presentation Foundation, LINQ to SQL, Visual Basic and a WPF DataGrid. I can add and modify rows in the DataGrid and save them to my database, but when I programmatically insert new rows into the DataGrid and modify them I can no longer save them back to my database; I get the error message "System.Data.Linq.ChangeConflictException: Row not found or changed." Can someone tell me what I need to do in order to be able to modify the inserted rows and save them back to my database?

    Bob




    Robert Agans
    • Moved by Mike FengModerator Thursday, May 5, 2011 7:33 AM Data (From:Visual Basic Express Edition)
    Monday, May 2, 2011 3:40 AM

Answers

  • Hi Serguey123

    I have no trouble inserting the information contained in my code into the PayrollEntry database table. The error develops when I add the job number and payroll regular, overtime and double time hours into the WPF PayrollEntry DataGid and then try to save it. The error states that LINQ to SQL cannot find the row or that it has been changed, and of course I have added to it but have not changed the original entry and the date stays the same.

    Also, the only use of optimistic or pessimistic concurrency that I could find is when you are using datasets and I am not using datasets, I am using a WPF DataGrid with LINQ to SQL and working directly with my database, and I couldn’t find any provision for using optimistic concurrency in this situation.

    Thanks for your input, Bob


    I just found out that if I fill in all the columns programmically, then I can make changes and save them back to my database without any problem.

    Thanks to all who contributed in this effort.  You can mark this request as answered.

    Robert Agans

    • Marked as answer by BobOfCalif Wednesday, May 11, 2011 8:17 PM
    Wednesday, May 11, 2011 7:34 PM

All replies

  • Hi,


    Please go through


    http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx
    http://msdn.microsoft.com/en-us/library/bb386944.aspx


    Regards, Anoop
    Monday, May 2, 2011 7:15 AM
  • Hi Anoop,Thanks for responding to my request!

    The first link you provide is concerning DataSets which uses AcceptChanges and UpDate methods, but I am using LINK to SQL tied directly to my database, not DataSets. LINQ to SQL uses the SubmitChanges method to save you changes back to the database.

    The second link you provided could not be opened.

    Bob


    Robert Agans
    Monday, May 2, 2011 8:47 PM
  • Hi Bob,

    Welcome to the MSDN Forum.

    I have moved this thread to Linq to Sql for more effective responses.

    Thank you for understanding and support.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 5, 2011 7:35 AM
    Moderator
  • Hello, can you provide the code for the insertion and the submition?

    Regards

    Friday, May 6, 2011 1:04 PM
  • Ok, first, that post I can't read well because the font is really small.  Second I'm not a VB expert but that code doesn't seem great.

     I think you forgot to add it to your table using Add()

    Read this http://msdn.microsoft.com/en-us/library/bb386931.aspx

    Regards

    Monday, May 9, 2011 5:43 PM
  • Here's a larger font, but I can't use the Add() statement.

    Bob

    Private Sub btnPayNewRows_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPayNewRow.Click
        'Declare varitables for PayrollEntry table (When I change any of these rows after inserting them and try to save them again, I get the error message "Row not found or changed")
    
        Dim strDate As String = "2/14/1998"
        Dim strSSNum As String = ""
        Dim strLName As String = ""
        Dim strPScale As String = ""
        Dim intRNum As Integer = 0
        Dim i As Integer = 0
    
        For i = 1 To 2
          Select Case i
            Case 1
              intRNum = i
              strSSNum = "300-44-3220"
              strLName = "Agans"
              strPScale = "14.47"
            Case 2
              intRNum = i
              strSSNum = "310-66-4575"
              strLName = "Azua"
              strPScale = "11.01"
          End Select
    
          'Insert rows into PayrollEntry
          Dim newPayrollEntry = New PayrollEntry With {.RowN = intRNum, .DateOf = strDate, .SocialSecurityNum = strSSNum, .LastName = strLName, .PayScale = strPScale}
          dc.PayrollEntries.InsertOnSubmit(newPayrollEntry)
          dc.SubmitChanges()
        Next
    
      End Sub

    Robert Agans
    Tuesday, May 10, 2011 2:12 AM
  • Ohh, ok, thanks for the enlargement... this  {Dim strDate As String = "2/14/1998"} is the culprit I think, the problem is that linq has difficulties trying to update high precission field with optimistic concurrency as there is a loss of precision when this is translated to sql and the update fails because there is no apparent change between the two sets of data.

    You can disable the optimistic concurrency for the datetime field and that should eliminate the problem

    Regards

    Tuesday, May 10, 2011 3:03 PM
  • Hi Serguey123

    I have no trouble inserting the information contained in my code into the PayrollEntry database table. The error develops when I add the job number and payroll regular, overtime and double time hours into the WPF PayrollEntry DataGid and then try to save it. The error states that LINQ to SQL cannot find the row or that it has been changed, and of course I have added to it but have not changed the original entry and the date stays the same.

    Also, the only use of optimistic or pessimistic concurrency that I could find is when you are using datasets and I am not using datasets, I am using a WPF DataGrid with LINQ to SQL and working directly with my database, and I couldn’t find any provision for using optimistic concurrency in this situation.

    Thanks for your input, Bob


    I just found out that if I fill in all the columns programmically, then I can make changes and save them back to my database without any problem.

    Thanks to all who contributed in this effort.  You can mark this request as answered.

    Robert Agans

    • Marked as answer by BobOfCalif Wednesday, May 11, 2011 8:17 PM
    Wednesday, May 11, 2011 7:34 PM