none
Linq and BindingSource.Remove problem

    Question

  • Hi

    I am using BindingSource and Linq to SQL, when i click button Add i am calling BindingSource.AddingNew which adds an object in DataContext ( inserts list )

    everything is working fine, unless i tried to cancel the adding buy clicking delete button for the new added row, the row will be removed from the BindingSource List button it stays in the inserts list in DataContext, which generates errors is some fields are not filled

    how to solve this issue

    thanks in advance

    regards

    Saturday, August 07, 2010 12:47 AM

Answers

  • Hi

    thanks for the reply

    he is the idea, when i call tDegreesBindingSource.RemoveAt(row.Index) the record is deleted successfully from the datagridview, but when i call SubmitChanges it generate error i can't insert null values to some  filled which they don't allow the null value.

    i investegated the problem by this way

    ChangeSet set=context.GetChangeSet();

    i found that the record i removed using the BindingSource.RemoveAt(row.Index) still in the inserts list and didn't removed.

    the problem is that i didn't save this record yet in the database and i want to cancel the inserting which i don't know any way to do this, to remove this record from the inserts, and my form contains many grids that will allow the user to add as much as he wants from data and then to submit all the changes from all grids to the database.

    what i need is to know how to tell the DataContext to cancel the insert for a specific objects

     

    hope i explained my problem in correct way now

    regards

    http://xacc.wordpress.com/2009/02/17/datacontextrevertchanges/
    Just Be Humble Malange!
    • Marked as answer by Hadi2008 Saturday, August 07, 2010 1:42 PM
    Saturday, August 07, 2010 11:51 AM

All replies

  • Hello,

     Plz tell me the type of your datacontext.

     

    Saturday, August 07, 2010 7:30 AM
  • Iam using L2S DBML

    here a sample of code i am using ( tables are in this way)

    //TApplicant ( master table)
    //TDegree ( detail table)
    
    private void degreeAddButton_Click(object sender, e EventArgs)
    {
       bool hault=false;
       if(tDegreesBindingSource.Count>0)
       {
          TDegree degree=tDegreesBindingSource[tDegreesBindingSource.Count-1] as TDegree;
          degree.GetRuleViolations();
          if(!degree.IsValid)
              hault=true; 
       }
       if(!hault)
         tDegreesBindingSource.AddNew();
    }
    
    private void degreeDeleteButton_Click(object sender,e EventArgs)
    {
        if(degreeGridView.SelectedRows!=null)
        {
           if(degreeGridView.SelectedRows.Count>0)
           {
               DialogResult result= MessageBox.Show("Are you sure you want to proceed in deletion?","Confirmation",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
              if(result==DialogResult.Yes)
              {
                   foreach(DataGridViewRow row in degreeGridView.SelectedRows)
                      tDegreesBindingSource.RemoveAt(row.Index);
                   tDegreesBingingSource.EndEdit();
              }
           }
        }
    }
    

    this is generating errors for me when i add a record to the gridview and then clicking the delete button without filling any data in this record ( trying to undo the add!)

     

    recards

    Saturday, August 07, 2010 8:10 AM
  • What type of error? can u post here please, just the error ?

    Thanks


    Just Be Humble Malange!
    Saturday, August 07, 2010 11:02 AM
  • I just gonna post you a code that I created long ago related to delete command from dgv.

     private void button3_Click(object sender, EventArgs e)
        {
          if (MessageBox.Show("Do you really want to delete the selected record(s)?", "Delete records", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false) == DialogResult.Yes)
          {
            {
              int cnt = dataGridView1.SelectedRows.Count;
              for (int i = 0; i <= cnt - 1; i++)
              {
                this.dataGridView1.Rows.RemoveAt(rowToBeSelected);
                {
                  DataTable dt = ds.Tables["Table_Inf"];
                  this.dataGridView1.BindingContext[dt].EndCurrentEdit();
                  this.da.Update(dt);
                }
                
                }
               }
             }
           }
    You can change it as you want.


    Just Be Humble Malange!
    Saturday, August 07, 2010 11:07 AM
  • Hi

    thanks for the reply

    he is the idea, when i call tDegreesBindingSource.RemoveAt(row.Index) the record is deleted successfully from the datagridview, but when i call SubmitChanges it generate error i can't insert null values to some  filled which they don't allow the null value.

    i investegated the problem by this way

    ChangeSet set=context.GetChangeSet();

    i found that the record i removed using the BindingSource.RemoveAt(row.Index) still in the inserts list and didn't removed.

    the problem is that i didn't save this record yet in the database and i want to cancel the inserting which i don't know any way to do this, to remove this record from the inserts, and my form contains many grids that will allow the user to add as much as he wants from data and then to submit all the changes from all grids to the database.

    what i need is to know how to tell the DataContext to cancel the insert for a specific objects

     

    hope i explained my problem in correct way now

    regards

    Saturday, August 07, 2010 11:23 AM
  • Hi

    thanks for the reply

    he is the idea, when i call tDegreesBindingSource.RemoveAt(row.Index) the record is deleted successfully from the datagridview, but when i call SubmitChanges it generate error i can't insert null values to some  filled which they don't allow the null value.

    i investegated the problem by this way

    ChangeSet set=context.GetChangeSet();

    i found that the record i removed using the BindingSource.RemoveAt(row.Index) still in the inserts list and didn't removed.

    the problem is that i didn't save this record yet in the database and i want to cancel the inserting which i don't know any way to do this, to remove this record from the inserts, and my form contains many grids that will allow the user to add as much as he wants from data and then to submit all the changes from all grids to the database.

    what i need is to know how to tell the DataContext to cancel the insert for a specific objects

     

    hope i explained my problem in correct way now

    regards

    http://xacc.wordpress.com/2009/02/17/datacontextrevertchanges/
    Just Be Humble Malange!
    • Marked as answer by Hadi2008 Saturday, August 07, 2010 1:42 PM
    Saturday, August 07, 2010 11:51 AM
  • Hi

    Bingo,  cheers thanks for the reply

    it helped me, thanks and here what i did

    i created partial class from DataContext and i added this method to it

    public void RemoveOnInsert<T>(T entity)
    {
       var t=entity.GetType();
       GetTable(t).DeleteOnSubmit(entity);
    }
    Saturday, August 07, 2010 1:42 PM