views:

23

answers:

4

I have the following code:

        Dim dbHousing As New dcHousingDataContext
    Dim pullresidents = From p In dbHousing.webResidents _
                Select p.dorm_building, p.dorm_room, p.occupantnum _
                Order By dorm_building, dorm_room
    Dim j as integer = 1
    Dim previous As String = ""
    For Each row In pullresidents
        If previous = "" Then
            Dim wResident As New webResident
            wResident.occupantnum = j
            dbHousing.SubmitChanges()
            j = j + 1
            previous = row.dorm_building & " " & row.dorm_room
        Else
            If previous = row.dorm_building & " " & row.dorm_room Then
                Dim wResident As New webResident
                wResident.occupantnum = j
                dbHousing.SubmitChanges()
                j = j + 1
            Else
                Dim wResident As New webResident
                j = 1
                wResident.occupantnum = j
                dbHousing.SubmitChanges()
                j = j + 1
                previous = row.dorm_building & " " & row.dorm_room
            End If
        End If

    Next

When I run it, looking at the debugger everything looks okay but when I check the database table it is supposed to be inserting into - the column values (for occupantnum) haven't changed - they are all still null!

Not sure if this is related, but note that I've now updated j to explicitly be an integer. I noticed while debugging that wResident.occupantnum was appearing as Type "Integer?" - but this doesn't seem to have made any difference - when I rerun it it still says Type "Integer?"

+1  A: 

Forgive me if this is wrong - but don't you have to associate the newly created "webResident" objects with the context if you're to save them? I can't see how dbHousing.SubmitChanges() could save webResidents that it's not aware of.

Will A
See my comment on Daniel's answer above...I'm not comprehending how to associate this to the current context.
davemackey
+2  A: 

You iterate over all fetched objects with For Each row In pullresidents but you never modify row. So there are no changes that could be send back to the database.

Daniel Brückner
How do I tell it to associate the occupantnum I'm trying to change with that row's record? If I do row.occupantnum = j it says that row is read-only?
davemackey
This is because you perform a projection to an anonymouse type with `Select p.dorm_building, p.dorm_room, p.occupantnum`. Remove this projection to fetch the complete entity (just use `Select p`) and it will be updatable.
Daniel Brückner
use IQueryable<T>.ToArray() and then access the array using an index... then you can modify the values
youllknow
Dan is right, instead of Select p.dorm_building, p.dorm_room, p.occupantnum _, just do Select p
Rafe
Daniel - Seriously, you are my hero!
davemackey
`ToArray()` will not help because the anonymouse types have readonly properties. Further the context knows nothing about the anonymouse type and cannot use it to update the database anyway even if the properties would be writable.
Daniel Brückner
+1  A: 

Dave, you need to insert you new webResident objects into the table first before you submit changes.

dbHousing.webResidents.InsertOnSubmit(webResident);

Rafe
Thanks Rafe. I'm actually trying to update the existing row objects I've pulled - not add new objects...I'm missing something?
davemackey
I think you are doing too much here. You just want to change the row, correct?Try making this:Dim wResident As New webResident wResident.occupantnum = j dbHousing.SubmitChanges() j = j + 1into this: row.occupantnum = j dbHousing.SubmitChanges() j = j + 1
Rafe
If you want to update the objects then you have to modify the objects themselves, not create new ones - I think you want to set row.occupantnum = <value>.
Will A
I receive the error "Property 'occupantnum' is 'ReadOnly'
davemackey
+2  A: 

I think you are doing too much here.

You just want to change the row, correct?

Try making this:

Dim wResident As New webResident 
wResident.occupantnum = j 
dbHousing.SubmitChanges() 
j = j + 1 

into this:

row.occupantnum = j 
dbHousing.SubmitChanges() 
j = j + 1
Rafe