By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

Concurrency violation (new try)

P: n/a
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[i].ReadOnly)
dr[i] =
this.ldb.DohvatiDataSet.Tables[tableName].Columns[i].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().Equa ls("<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!
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

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" <vl************@fer.hr> wrote in message
news:dj**********@bagan.srce.hr...
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[i].ReadOnly)
dr[i] =
this.ldb.DohvatiDataSet.Tables[tableName].Columns[i].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().Equa ls("<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!

Nov 17 '05 #2

P: n/a
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 )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:#n**************@tk2msftngp13.phx.gbl...

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" <vl************@fer.hr> wrote in message
news:dj**********@bagan.srce.hr...
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[i].ReadOnly)
dr[i] =
this.ldb.DohvatiDataSet.Tables[tableName].Columns[i].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().Equa ls("<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!


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.