Concurrency violation (new try)

V

Vladimir O¾ura

I am building a pocket pc application that requires a datagrid. I am
inserting a new row this way:


private void mInsert_Click(object sender, System.EventArgs e)
{
try
{
DataRow dr = this.ldb.DohvatiDataSet.Tables[tableName].NewRow();
dr.BeginEdit();
for(int i = 0; i <
this.ldb.DohvatiDataSet.Tables[tableName].Columns.Count; i++)
if(!this.ldb.DohvatiDataSet.Tables[tableName].Columns.ReadOnly)
dr =
this.ldb.DohvatiDataSet.Tables[tableName].Columns.DefaultValue;
if(!this.cbImena.SelectedItem.ToString().Equals("<SVE OSOBE>"))
dr["Osoba"] = this.cbImena.SelectedItem.ToString();
if(!this.cbProjekt.SelectedItem.ToString().Equals("<SVE PROJEKTE>"))
dr["KratProjekt"] = this.cbProjekt.SelectedItem.ToString();
if(!this.cbVrstePosla.SelectedItem.ToString().Equals("<SVE VRSTE
POSLA>"))
dr["KratPosao"] = this.cbVrstePosla.SelectedItem.ToString();
if(this.checkBox1.Checked)
dr["DatPosao"] = this.dtp.Value.ToString();
dr.EndEdit();
this.ldb.DohvatiDataSet.Tables[tableName].Rows.Add(dr);
this.mSave_Click(this, System.EventArgs.Empty); // defined at the bottom
of this method
this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Sort = "";
this.dataGrid1.CurrentRowIndex =
this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Count - 1;
this.dataGrid1.ScrollToLeft();
}
catch(System.Data.DBConcurrencyException dbce)
{
MessageBox.Show(dbce.Message.ToString(), "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}

private void mSave_Click(object sender, System.EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.Deleted));

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.ModifiedCurrent));

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.Added));
this.ldb.DohvatiDataSet.AcceptChanges();
this.dataGrid1.Focus();
Cursor.Current = Cursors.Default;
this.UpdateAgreg();
}
catch(System.Data.DBConcurrencyException dbce)
{
if(Cursor.Current == Cursors.WaitCursor) Cursor.Current =
Cursors.Default;
MessageBox.Show(dbce.Message.ToString(), "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}

I only get a concurrency violation when I insert a new row and change data
in it, that save it. If no new rows are inserted I don't get a concurrency
violation. Please help!

Vladimir O¾ura!
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


I confess you that I did not fully read the code, but I bet that the
problem is that you are trying to insert a new row in the very same
collection you are iterating.
You have to store the new rows in a temp collection (like ArrayList) and
then outside the loop add them to the collection.

like:
ArrayList ar = new ArrayList()
foreach( DataRow row in Rows )
{
if ( addthis)
ar.Add( row)
}
foreach( DataRow row in ar)
Rows.Add( row);


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Vladimir O¾ura said:
I am building a pocket pc application that requires a datagrid. I am
inserting a new row this way:


private void mInsert_Click(object sender, System.EventArgs e)
{
try
{
DataRow dr = this.ldb.DohvatiDataSet.Tables[tableName].NewRow();
dr.BeginEdit();
for(int i = 0; i <
this.ldb.DohvatiDataSet.Tables[tableName].Columns.Count; i++)
if(!this.ldb.DohvatiDataSet.Tables[tableName].Columns.ReadOnly)
dr =
this.ldb.DohvatiDataSet.Tables[tableName].Columns.DefaultValue;
if(!this.cbImena.SelectedItem.ToString().Equals("<SVE OSOBE>"))
dr["Osoba"] = this.cbImena.SelectedItem.ToString();
if(!this.cbProjekt.SelectedItem.ToString().Equals("<SVE PROJEKTE>"))
dr["KratProjekt"] = this.cbProjekt.SelectedItem.ToString();
if(!this.cbVrstePosla.SelectedItem.ToString().Equals("<SVE VRSTE
POSLA>"))
dr["KratPosao"] = this.cbVrstePosla.SelectedItem.ToString();
if(this.checkBox1.Checked)
dr["DatPosao"] = this.dtp.Value.ToString();
dr.EndEdit();
this.ldb.DohvatiDataSet.Tables[tableName].Rows.Add(dr);
this.mSave_Click(this, System.EventArgs.Empty); // defined at the
bottom
of this method
this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Sort = "";
this.dataGrid1.CurrentRowIndex =
this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Count - 1;
this.dataGrid1.ScrollToLeft();
}
catch(System.Data.DBConcurrencyException dbce)
{
MessageBox.Show(dbce.Message.ToString(), "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}

private void mSave_Click(object sender, System.EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.Deleted));

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.ModifiedCurrent));

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.Added));
this.ldb.DohvatiDataSet.AcceptChanges();
this.dataGrid1.Focus();
Cursor.Current = Cursors.Default;
this.UpdateAgreg();
}
catch(System.Data.DBConcurrencyException dbce)
{
if(Cursor.Current == Cursors.WaitCursor) Cursor.Current =
Cursors.Default;
MessageBox.Show(dbce.Message.ToString(), "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}

I only get a concurrency violation when I insert a new row and change data
in it, that save it. If no new rows are inserted I don't get a concurrency
violation. Please help!

Vladimir O¾ura!
 
V

Vladimir O¾ura

I am not trying to insert a new row in the very same collection I am
iterating. Here are the steps I take to insert a new row:

1. Make a new row with the NewRow method.
2. Set the value of each column to the column default value for the new row
(this is where I iterate through the columns collection, not the rows
collection)
3. Add the new row with the Add method.
4. Call the Update method of the SqlCeDataAdapter
5. Call the AcceptChanges method.

Vladimir O¾ura



Ignacio Machin ( .NET/ C# MVP ) said:
Hi,


I confess you that I did not fully read the code, but I bet that the
problem is that you are trying to insert a new row in the very same
collection you are iterating.
You have to store the new rows in a temp collection (like ArrayList) and
then outside the loop add them to the collection.

like:
ArrayList ar = new ArrayList()
foreach( DataRow row in Rows )
{
if ( addthis)
ar.Add( row)
}
foreach( DataRow row in ar)
Rows.Add( row);


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Vladimir O¾ura said:
I am building a pocket pc application that requires a datagrid. I am
inserting a new row this way:


private void mInsert_Click(object sender, System.EventArgs e)
{
try
{
DataRow dr = this.ldb.DohvatiDataSet.Tables[tableName].NewRow();
dr.BeginEdit();
for(int i = 0; i <
this.ldb.DohvatiDataSet.Tables[tableName].Columns.Count; i++)
if(!this.ldb.DohvatiDataSet.Tables[tableName].Columns.ReadOnly)
dr =
this.ldb.DohvatiDataSet.Tables[tableName].Columns.DefaultValue;
if(!this.cbImena.SelectedItem.ToString().Equals("<SVE OSOBE>"))
dr["Osoba"] = this.cbImena.SelectedItem.ToString();
if(!this.cbProjekt.SelectedItem.ToString().Equals("<SVE PROJEKTE>"))
dr["KratProjekt"] = this.cbProjekt.SelectedItem.ToString();
if(!this.cbVrstePosla.SelectedItem.ToString().Equals("<SVE VRSTE
POSLA>"))
dr["KratPosao"] = this.cbVrstePosla.SelectedItem.ToString();
if(this.checkBox1.Checked)
dr["DatPosao"] = this.dtp.Value.ToString();
dr.EndEdit();
this.ldb.DohvatiDataSet.Tables[tableName].Rows.Add(dr);
this.mSave_Click(this, System.EventArgs.Empty); // defined at the
bottom
of this method
this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Sort = "";
this.dataGrid1.CurrentRowIndex =
this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Count - 1;
this.dataGrid1.ScrollToLeft();
}
catch(System.Data.DBConcurrencyException dbce)
{
MessageBox.Show(dbce.Message.ToString(), "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}

private void mSave_Click(object sender, System.EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.Deleted));

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.ModifiedCurrent));

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.Added));
this.ldb.DohvatiDataSet.AcceptChanges();
this.dataGrid1.Focus();
Cursor.Current = Cursors.Default;
this.UpdateAgreg();
}
catch(System.Data.DBConcurrencyException dbce)
{
if(Cursor.Current == Cursors.WaitCursor) Cursor.Current =
Cursors.Default;
MessageBox.Show(dbce.Message.ToString(), "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}

I only get a concurrency violation when I insert a new row and change data
in it, that save it. If no new rows are inserted I don't get a concurrency
violation. Please help!

Vladimir O¾ura!

 
Top