Follow me on Twitter RSS FEED
Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

Row not found or changed

Row not found or changed


Recently I worked with linq. Linq makes the record insertion, deletion and update much easier in to a database. So I really found very easy in coding it.

The enjoyment not last for a day. Suddenly faced an error “Row not found or changed” while updating a record. But there is no problem with insertion or deletion.

Searched in all forums. People responded with

  • Check if you have given the primary key for table
  • Delete and recreate the dbml 

Did all those but didn’t work for me. I told to my colleague about this issue. Even he asked me if I did above 2 things. Then I showed the code. Below is my code which raised error.


void update(int _ID,String _Name,String _Age)
{
StudentTable _StudentTable = new StudentTable();
_StudentTable.ID=_ID;
_StudentTable.Name=_Name;
_StudentTable.Age=_Age;
using (DataContext dc = new DataContext(ConnectionString))
{
dc.StudentTables.InsertOnSubmit(_StudentTable);
dc.SubmitChanges();
}
}


And he simply laughed at me and told the basic about linq. That every record will have a unique indentifier other than primary key.  In my case ,used a TimeStamp so that it will avoid concurrency issue. Well so for the update he told me I should have first retrieved the record and changed the respective fields in it.

Below is the working code.

void update(int _ID,String _Name,String _Age)
{
StudentTable _StudentTable ;
connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
using (DataContext dc =new DataContext(connString)
{
_ StudentTable = (from u in dc.StudentTables
where u.ID == ID
select u).FirstOrDefault();
}
_StudentTable.ID=_ID;
_StudentTable.Name=_Name;
_StudentTable.Age=_Age;
using (DataContext dc = new DataContext(ConnectionString))
{
dc.StudentTables.InsertOnSubmit(_StudentTable);
dc.SubmitChanges();
}
}

Really nice experience with linq.

Have a cheerful day