tags:

views:

182

answers:

1

Hello,

I have a editable gridview in my aspx page. Users can enter records and after hitting submit using LinQ I am inserting the records to the database. It is working absolutely fine.

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()

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

How can I do that using LinQ

+1  A: 

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()
Jon Freeland