I have the following code, that in my head should create a new row in a table:
Dim fin As Boolean
Dim db2 As New ChecklistModeldbmlDataContext
For Each a In bServers
If collection("fin" & a.ServerName) = "true, false" Or collection("fin" & a.ServerName) = "true,false" Then
fin = True
Else
fin = False
End If
bLog.AMLogID = amLog.LogID
bLog.ByteCount = collection("bytes" & a.ServerName)
bLog.DurationHours = collection("hours" & a.ServerName)
bLog.DurationMinutes = collection("minutes" & a.ServerName)
bLog.DurationSeconds = collection("seconds" & a.ServerName)
bLog.IsFinished = fin
bLog.ServerID = a.ServerID
bLog.DetailsAndErrors = collection("details" & a.ServerName)
db2.BackupLogs.InsertOnSubmit(bLog)
db2.SubmitChanges()
Next
It only ever adds one record in to the table and then errors with Cannot add an entity that already exists.
Now it should enter 4 rows into the table, but I can’t work out why the above gives me that error.
I’ve also tried having the db2.SubmitChanges() outside of the for each and it just inserts the last row.
Any thoughts?
You’re not creating a new bLog in your loop. In each iteration, you’re just modifying the data of a single record.
Try using the
newkeyword to create a new instance of bLog before you assign data to it in your ForEach loop