none
InsertOnSubmit duplicates a record RRS feed

  • Question

  •         EntitiesDataContext dc = new EntitiesDataContext(); 
            bool canceled; 
            private void Form1_Load(object sender, EventArgs e) 
            { 
                this.dataGridView1.DataSource = dc.Table1s; 
                this.dataGridView1.Columns[0].Visible = false
     
                //set events 
                this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit); 
      this.dataGridView1.CancelRowEdit += new System.Windows.Forms.QuestionEventHandler(this.dataGridView1_CancelRowEdit);
            } 
            private void dataGridView1_CancelRowEdit(object sender, QuestionEventArgs e) 
            { 
                canceled = true
            } 
     
            private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
            { 
                if (!canceled) 
                { 
                    string val = ((System.Windows.Forms.DataGridView)(sender)).Rows[e.RowIndex].Cells[1].Value.ToString(); 
     
                    this.dc.Table1s.InsertOnSubmit(new Table1 
                    { 
                        text = val 
                    }); 
                    
                    dc.SubmitChanges(); 
                } 
                else 
                    dc = new EntitiesDataContext(); 
            } 
    Hello...
    I have a table(Table1) inside a database with 2 columns: an id(PK, int, autoincrement) and a text(nvarchar(50)).

    On a Windows Form I have a DataGridView and on CellEndEdit, I insert the new added value into the Table1.

    The problem is that inside the table are 2 rows with the same data but with diffrent Ids.

    Why is that?

    Thanks...

            

    Eusebiu
    Sunday, January 11, 2009 12:43 AM