acadia
acadia

Reputation: 2333

Update list of records using LINQ

I have a editable GridView on my webforms page. Users can enter records and after hitting submit using LINQ I am inserting the records to the database. It is working absolutely fine.

I am not able to figure out how to update the records. I am populating the records in the GridView once the user hits the submit button, I need to update the existing records.

How can this be done?

Dim db as new empDBDataContext
Dim rw As GridViewRow
Dim emp as new employee
emp.name="test"
emp.city="NYC"
emp.age=40
For each rw in GridView1.Rows
   Dim cert as new certifications
   cert.Name=CType(rw.FindControl("lblCert"), TextBox).Text
   cert.score=CType(rw.FindControl("lblScore"), TextBox).Text     
   emp.cert.add(cert)
Next

db.emp.insertonsubmit(emp)
db.submitChanges()

Upvotes: 0

Views: 843

Answers (1)

Jon
Jon

Reputation: 4945

You can use your unique identifier for certifications to select the rows from the database (I used lblId as the control name). Then, set the properties to the updated values. It's very similar to what you're doing for the insert. Please excuse my VB syntax ignorance...

For each rw in GridView1.Rows
 // Load the cert based on the ID of the row.
 Dim cert
 cert = (from c in db.certifications
        where c.Id = (int) CType(rw.FindControl("lblId"), TextBox).Text).Single()
 // Update the values.
 cert.Name=CType(rw.FindControl("lblCert"), TextBox).Text
 cert.score=CType(rw.FindControl("lblScore"), TextBox).Text
Next

// Submit all the changes.
db.submitChanges()

Upvotes: 1

Related Questions