Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Problem With updating record using linq...
 

Problem With updating record using linq...

Hi,

Im using Linq to Update Record like this:

public bool update(string name)   
        {   
            try  
            {   
                var query = from Album in db.Albums   
                            where Album.id == id   
                            select Album;   
  
                foreach (Album Al in query)   
                {   
                    Al.name = name;   
                }   
                db.SubmitChanges();   
  
                return true;   
            }   
            catch  
            {   
                return false;   
            }   
        }  
My problem is:

After I updated the name,

Im getting the Data again

and I get the Old Name, not the New Name.

What I do Wrong?

Thanks...

gb_007  Wednesday, August 05, 2009 9:19 AM

Hi gb_007,

Sorry for the misunderstanding. I made a test project and met the same issue. The root cause I found is that the table has no primary key. I add a primary key and the data is updated successfully. You can add a primary key to the table in database and recreate the .dbml file to see if it works.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer bygb_007 Friday, August 07, 2009 1:41 PM
  •  
Aland Li  Friday, August 07, 2009 12:48 PM

Hi,

Im using Linq to Update Record like this:

public bool update(string name)   
        {   
            try  
            {   
                var query = from Album in db.Albums   
                            where Album.id == id   
                            select Album;   
  
                foreach (Album Al in query)   
                {   
                    Al.name = name;   
                }   
                db.SubmitChanges();   
  
                return true;   
            }   
            catch  
            {   
                return false;   
            }   
        }  
My problem is:

After I updated the name,

Im getting the Data again

and I get the Old Name, not the New Name.

What I do Wrong?

Thanks...

gb_007  Wednesday, August 05, 2009 9:19 AM
Plz help me,

Thanks..
gb_007  Wednesday, August 05, 2009 9:41 PM
Plz help me,

Thanks..
gb_007  Wednesday, August 05, 2009 9:41 PM
I would assume you have one AlbumID for each album and not a collection of them.

Change this:
var query = from Album in db.Albums
where Album.id == id
select Album;

To this:
Album album = (from a in db.Albums
where a.id == id
select a).Single<Album>();
album.name = name;
try
{
db.SubmitChanges();
}
catch (ChangeConflictException)
{
db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Edited byJohnGrove Thursday, August 06, 2009 2:42 AM
  •  
JohnGrove  Wednesday, August 05, 2009 10:04 PM

Try it,

It still return me the Old Name.

When I Get it after Updating like u said.

WHAT TO DO?

Thanks..

gb_007  Thursday, August 06, 2009 7:48 AM
Plz help me!

Thanks..
gb_007  Thursday, August 06, 2009 7:49 AM

I might have made a mistake in syntax, since I was doing this from memory and not behind VS at the time.Can you try this before we continue?

var album = (from a in db.Albums
where a.id == id
select a).FirstOrDefault();
album.name = name;
try
{
db.SubmitChanges();
}
catch (ChangeConflictException)
{
db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
}


John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Thursday, August 06, 2009 3:00 PM
Still After Updating,

I'm getting the Old Data.

What else to do?

The DBML file - Copy to Output Directory property is set to copy if newer...

is it means something?

Thanks...
gb_007  Thursday, August 06, 2009 4:38 PM
Make sure you re-bind after the updation. If you are binding to a DataGridView or whatever.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Thursday, August 06, 2009 6:57 PM

Sure,

I set with it a ListView of Items.
With the albums name that I get from the DB.

gb_007  Thursday, August 06, 2009 7:18 PM

Hi gb_007,

You need to use DataAdapter to update the data to database. This is a similar thread:
http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/51dbe4c1-41eb-442f-8a74-89b37db03405.

This is a sample discusses the common operations via Linq to Sql:
http://www.codeproject.com/KB/linq/LINQToSQLBaseCRUDClass.aspx.

You can get more information about DataAdapter from:
http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx.

You can get more information about Linq to Sql from:
http://msdn.microsoft.com/en-us/library/bb425822.aspx.

Best regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 07, 2009 3:36 AM
Also,

tried add this:

db.Refresh(

RefreshMode.OverwriteCurrentValues, db.Albums);
db.SubmitChanges();

It updates in DB.

But next Time I asked the same Album Name from DB,
I get the OLD name.

That's my problem.
Got it?

What to do?
Thanks..

gb_007  Friday, August 07, 2009 8:51 AM

Hi gb_007,

Sorry for the misunderstanding. I made a test project and met the same issue. The root cause I found is that the table has no primary key. I add a primary key and the data is updated successfully. You can add a primary key to the table in database and recreate the .dbml file to see if it works.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer bygb_007 Friday, August 07, 2009 1:41 PM
  •  
Aland Li  Friday, August 07, 2009 12:48 PM
My All DB Tables Got PK....

WhenI Update the DATA It's updating it...

But when I'm getting back the Album Data. I'm getting the Old Data with the Old Name

That's my problem..

What u think I can do?

Thanks..
gb_007  Friday, August 07, 2009 1:12 PM

You can use google to search for other answers

Custom Search

More Threads

• Datagrid and Web Service
• best way to handle a real-time time-remaining column in DGV
• Regarding Array as DataSource to ComboBox
• Datagridview checkbox column, .net 2.o
• datagridview cell will not disappear !
• DataGridView
• How to associate datagridview with list<>
• fill the combobox with a field
• Attention NEWBIES... updating records
• IList<T> and BindingSource problem