SubmitChanges() not updating my db

jwcoleman87

Well-known member
Joined
Oct 4, 2014
Messages
124
Programming Experience
Beginner
    Public Sub UpdateRepair(SerialNumber As String, EndTime As DateTime,
                              Status As String, CSRQuoteFlag As Integer)
        Using db As New ProductionDataModelDataContext
            Try
                Dim UpdateCommand = (From repair In db.Repairs _
                                     Where repair.SerialNumber = SerialNumber).ToList()(0)
                UpdateCommand.Status = Status
                UpdateCommand.CSRQuoteFlag = CSRQuoteFlag
                UpdateCommand.EndTime = EndTime.Date

                db.SubmitChanges()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Using
    End Sub


This code executes, changes all the data as I step through, but something seems to just not click when the submitchanges() call goes out. Nothing actually happens. I'm not certain why this is happening, I've been googling all over.

Help?
 
If you want the first element, use .First() or .FirstOrDefault() (returns a null if the sequence is empty instead of generating an exception).

        Try                         
            Dim selectedRepair = db.Repairs.Where(Function(r) r.SerialNumber = SerialNumber).First
            selectedRepair.Status = Status
            selectedRepair.CSRQuoteFlag = CSRQuoteFlag
            selectedRepair.EndTime = EndTime.Date
            db.SubmitChanges()            
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

I don't see any reason why this wouldn't work, provided there is in fact a record by that serial number. If not you should see a Null Reference exception messagebox.
 
Yep, that's what it was, my primary key was not set here for some reason:

Capture.PNG

After setting the primary key, the update worked without a hitch!
 
Back
Top