views:

1947

answers:

4

hi, im trying to update all records in a sql table using the following code but no data is updated. does anyone know why?

        using (DataContext db = new DataContext())
        {
            foreach (Item record in db.Items)
            {
                record.Description += "bla";
                db.SubmitChanges();
            }
        }

code for setter:

[Column(Storage="_Description", DbType="NVarChar(400) NOT NULL", CanBeNull=false)] 
public string Description
{ 
  get { return this._Description; }
  set { 
      if ((this._Description != value))
       { 
         this._Description = value; 
       }
      }
}
+4  A: 

Out of curiosity, see if moving the SubmitChanges() outside of the loop makes a difference:

        using (DataContext db = new DataContext())
        {
            foreach (Item record in db.Items)
            {
                record.Description += "bla";   
            }
            db.SubmitChanges();
        }
BFree
thanks BFree but didnt work.
Grant
Nope, It wouldn't work out that way. It maybe improve efficiency.
Braveyard
+2  A: 

Maybe you need to supply a connection string.

using (DataContext db = new DataContext(_MyConnectionString))
{
    foreach (Item record in db.Items)
    {
        record.Description += "bla";
    }
    db.SubmitChanges();
}

I've had some strange issues with the DataContext when not supplying a connection string.

sshow
+2  A: 

I will point out you can SubmitChanges after the loop closes... This won't solve your problem but it will help out a bit.

tsilb
+3  A: 

From the details of the setter you have posted in the comments, your Description property has not been correctly created for notification of property changes. Did you write the property yourself or was it generated by the VS2008 tooling?

Your Item class (all Linq to Sql entities for that matter should implement INotifyPropertyChanging and INotifyPropertyChanged) which will give you both PropertyChanging event and PropertyChanged events, if you used the VS2008 tools you should get a couple of methods like the following in your entity classes:

    protected virtual void SendPropertyChanging(string propertyName)
    {
        if (this.PropertyChanging != null)
        {
            this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

    protected virtual void SendPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Now within your setter of your property you should use these methods to raise the required events:

[Column(Name="Description", Storage="_Description",
                  DbType="NVarChar(400) NOT NULL", CanBeNull=false)]   
public string Description
{
     get { return this._Description; }
     set
     {
         if ((this._Description != value))
         {
             this.SendPropertyChanging("Description");
             this._Description = value;
             this.SendPropertyChanged("Description");
         }
     }
 }

I also noticed you don't have the Name property set in your column attribute so add it just in case (I have it included in my example assuming your column name is "Description").

Simon Fox
Because of the implemented code I couldn't see any error which can prevent from updating but the error must be in setter.
Braveyard
Hi Simon, the DBML data was created completely from within VS2008. After following your instructions i still cannot get any data to update and the changeset returns 0...
Grant
Maybe there is something ridiculously basic i have overlooked like no primary key or something really really simple...
Grant
Can you put a breakpoint in the SendPropertyChanging and SendPropertyChanged methods to check that the DataContext is registered for those notifications (i.e. the event properties are not null)?
Simon Fox
Aaah, yes if you have no Primary Key on your table that will explain why the INotifyChanging and INotifyChanged interfaces are not being emitted by the generator. This would have actually choked on anything other than Update i.e. Insert or Delete (I'm pretty sure :) so you would have noticed. You can get around this (if you can't modify the table) by setting the PrimaryKey property to true on one of the tables columns in the Linq designer. However this will still cause issues if there ends up being duplicate data in the table.
Simon Fox
I might take a fresh look at this later on.. thankyou all for you suggestions and advice.
Grant