Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi,

I am trying to delete record from an entity from my datacontext and use SubmitChanges() method to save this. so I used cascading deletes rule .but not working this and not save any change in database table.( used linq to sql class (dbml))
Here's the code that is supposed to delete:

XML
<connectionStrings>

  <add name="Store.Properties.Settings.StoreConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Store.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
  <add name="StoreEntities" connectionString="metadata=res://*/StoreEntityModel.csdl|res://*/StoreEntityModel.ssdl|res://*/StoreEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\Store.mdf;integrated security=True;connect timeout=30;user instance=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>


C#
StoreDataContext store_dc = new StoreDataContext(constr);



C#
try
{
    foreach (StuffRegTable stf in DtgridTbl)
    {
        if (stf.Checked == true)
        {
            var deletobject = (from rec in store_dc.Stuffs
                               where rec.BareCode == stf.BarCode
                               select rec).Single();
            DtgridTbl.Remove(stf);
            store_dc.Stuffs.DeleteOnSubmit(deletobject);
            store_dc.SubmitChanges();


        }
    }

}//try
catch(Exception ex)
{
    MessageBox.Show(ex.Message.ToString());
}
Posted
Updated 15-Jul-12 20:15pm
v5
Comments
bbirajdar 15-Jul-12 12:30pm    
Does your Stuffs property has SaveChanges() or SumbitChanges() method? like store_dc.Stuffs.SubmitChanges(); ?
loger21 15-Jul-12 17:14pm    
no. not exist this method .
Nisarg S Shah 18-Jul-12 9:02am    
Either the Table will not be having the column with Primary key or while deleting the record you might have forgot to select the primary key.
It will still allow you to select the data but insertion or deletion or updation will not take place until and unless the primary key column is used to perform any of the above said transactions
loger21 20-Jul-12 8:48am    
My Either Table have the column with Primary key.can you more explain with example code?
Nisarg S Shah 25-Jul-12 6:50am    
All i mean is that there can be a possibility that the table might not have any primary key being defined or may be possible that the primary key is not being used while deleting the record.

1 solution

I think that this line is unnecesary:
C#
DtgridTbl.Remove(stf);


Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900