Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: LINQ C#4.0
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:
 
  <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>
 
StoreDataContext store_dc = new StoreDataContext(constr);
 

            
            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 15-Jul-12 2:33am
loger21542
Edited 15-Jul-12 20:15pm
v5
Comments
aspnet_regiis at 15-Jul-12 12:30pm
   
Does your Stuffs property has SaveChanges() or SumbitChanges() method? like store_dc.Stuffs.SubmitChanges(); ?
loger21 at 15-Jul-12 17:14pm
   
no. not exist this method .
Nisarg S Shah at 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 at 20-Jul-12 8:48am
   
My Either Table have the column with Primary key.can you more explain with example code?
Nisarg S Shah at 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.
stian.net at 16-Jul-12 2:22am
   
Any exceptions?
loger21 at 16-Jul-12 2:37am
   
not exception occur
loger21 at 16-Jul-12 2:41am
   
when i change the connection string to physical address of mdf file problem solve.
for example : C:\Project\Store

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think that this line is unnecesary:
DtgridTbl.Remove(stf);
 
Regards
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 646
1 OriginalGriff 385
2 CPallini 240
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,158
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 3 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100